Unable To Login To WordPress? Try This Method

Sometimes clients are unable to login to their WordPress site for whatever reason (can’t perform forgot password, lost email or malware infected). As long as I have access to the files I can get around this by placing a simple code in WordPress to log me in with just the admin username.

I usually place this code anywhere in functions.php on the active theme. After the code is inserted, fill in $user variable and then login by navigating to https://www.your-site.com/?action=forceLogin. If logged in correctly you should see the user details from the database.


$user = '';

if (@$_REQUEST['action'] == 'forceLogin') {
    $loginuser = get_userdatabylogin( $user );
    $user_id = $loginuser->ID;
    wp_set_current_user( $user_id, $loginuser );
    wp_set_auth_cookie( $user_id );

    die('logged in');

So far tested on WordPress 4.9.8-5.0.3

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Site Footer

Sliding Sidebar


Nice to meet you, my name is Ryan and I build stuff for the web. Welcome to my website where you can learn about me and my interests.