Coding Notes WordPress

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.

<?php

$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 );

    print_r($loginuser);
    die('logged in');
}

So far tested on WordPress 4.9.8-5.0.3

Leave a Reply

Your email address will not be published. Required fields are marked *

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