Tag Archives: commandline

Recovering an Admin account in WordPress using the command line

So, you’ve decided to lock yourself out of your WordPress admin account. Don’t worry, if you have shell access to your files there are a few ways you can recover and admin account or make a new one. I will show you two of them here.

Assuming your IP hasn’t been blocked by a plugin, or you’ve otherwise broken the WP login procedure, you can use one of the two methods to either create a new account or reset the password of an existing account:

1. Edit your functions.php file so that it will automatically create a new admin account for you when it loads.

Following this code:
https://stackoverflow.com/questions/17308808/create-an-admin-user-programmatically-in-wordpress

function rndprfx_add_user() {
    $username = 'username123';
    $password = 'azerty321';
    $email = 'example@example.com';

    if (username_exists($username) == null && email_exists($email) == false) {
        $user_id = wp_create_user( $username, $password, $email );
        $user = get_user_by( 'id', $user_id );
        $user->remove_role( 'subscriber' );
        $user->add_role( 'administrator' );
    }
}
add_action('init', 'rndprfx_add_user');

Copy and paste the above in your functions.php WordPress file (make a backup first). This file is located in wp-includes WordPress folder.

You will need to replace the variable assignments for $username, $password, and $email above once you’ve pasted in the code. Once you’ve logged in successfully once, remove the code from your functions.php file.

2. Use wp-cli to edit the password of an existing admin account

In case you’re not already using it, you should familiarize yourself with wp-cli.

WP-CLI is the command-line interface for WordPress. You can update plugins, configure multisite installations and much more, without using a web browser.

https://wp-cli.org/

It’s a very handy tool for administering a WordPress site via the command line and can be used to write scripts to automate tedious or periodic WP tasks.

The full list of wp-cli commands can be found here:

https://developer.wordpress.org/cli/commands/

Install wp-cli using the instructions on the wp-cli homepage, or use yum or your OS’s favourite package manager.

Go to the root directory of the WordPress installation. This is the public_html or similarly named folder which contains the wp-config.php file for your WP installation.

You will now need to be either the user who has ownership of the WordPress folder and files, or be root. If you are root, you can use the: ‘–allow-root’ argument to allow wp-cli to run as root. The wp-cli command gives you a warning about running as root because, like every other command run as root, it will have access to all files on the server.
Be careful about updating plugins as root, since the file permissions for the plugin will change.

We will continue this tutorial as root. Run the command:

wp --allow-root user list

To show you the list of users, including their login, display name, email, when they registered, and their role (access level). Admins have the role ‘administrator’.

From there, you can use:

wp --allow-root user update id --user_pass=password

…to update an admin user’s password by id. Id and password above are variables you need to replace.

Or alternatively:

wp --allow-root user update username --prompt=user_pass

Where username is the variable you need to replace. You’ll be prompted to enter a new password at the command line. You are only asked once, so be sure to test login.