Creating a WordPress administrator account with the WP-CLI
As a developer you’re always going to need a login account for a WordPress website whether it’s for local testing or maintenance of a live website.
If you’ve set up your own WordPress website it’s likely that you already created an admin account but what if you’ve been brought in to an existing client project? In order to get set up you’ll probably request a copy of the WordPress theme files and the database to test out functionality locally.
Registering for an administrator account through the login page is not an option either as the administrator account must be created by an existing user with the same permissions. It’s very common to disable the account registration part of the website anyway as this can be a security risk.
Here are some examples of where creating an account would be problematic:
- The copy of the database doesn’t contain a login account that you can use
- You’ve forgotten your password or username and can’t login
- Creating a login directly in the database is not straightforward/possible
- No one is around to give you a login username/password
With the CLI you can create an account very quickly and easily.
Using the WordPress CLI
Via SSH
The following command can be run on a terminal either locally or connected remotely with SSH. The WP-CLI must be installed first.
wp user create username email@example.com --role=administrator
Where username
is something like anthony
Once the command above is run, there may be some warnings or errors displayed but at the bottom there will be a confirmation message with a new password like so
Success: Created user 2.
Password: 123456789abcdef
Make sure you store the password securely. I recommend 1Password. You can then login to your site with the username you typed above with the password.
When using Local by Flywheel
- In Local, right-click on a site and click “connect via SSH”
- Run the above command
- Save the generated password
When you can’t SSH/WP CLI is not installed
If you can’t SSH , you could execute a shell script
- Create a blank file called something like my-useradd.sh (see below)
- Set permissions to execute the file (
chmod +x my-useradd.sh
) - Run the file
./my-useradd.sh
#!/bin/bash
wp user create username email@example.com --role=administrator --user_pass=<password>
Notice I have included the user_pass
in this case- this is because once the script is run, there’s no way of seeing the generated password.
Once you’ve run the above script, you can login with the credentials written in the bash script. If the script is installed on a server you must delete it so other commands can’t be run in the script.