How To Create A WordPress Administrator User Using SQL

Creating a user in WordPress is usually a simple task, involving a couple clicks in the dashboard, some light typing, and a button click. But what if for some reason you’re locked out of the dashboard and can’t login?

SQL to the rescue!

If you have access to your WordPress database and can run some simple SQL commands you can easily create another user with administrator privileges.

Insert A User Into The WordPress Users Table

First, we need to insert our new user into the WordPress users table. For our purposes, we’ll only need to include a username, an MD5 encrypted password, a first and last name, and an email address.

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('adminuser', MD5('adminpassword'), 'Firstname Lastname', 'email@youremail.com', '0');

Give The New User Administrator Capabilities

Next, we give the newly created user administrator capabilities. Without this, the user is no different than a regular user.

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (SELECT max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');

Set The User Level To 10

Lastly, as with all administrator accounts, we need to set the user level to 10. You can learn more about user levels here.

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (SELECT max(id) FROM wp_users), 'wp_user_level', '10'); 

Putting It All Together

Change the following SQL to meet your requirements and run it on your WordPress database. By default, it will create a new user called adminuser with the password adminpassword with full administrator privileges.

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('adminuser', MD5('adminpassword'), 'Firstname Lastname', 'email@youremail.com', '0');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (SELECT max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (SELECT max(id) FROM wp_users), 'wp_user_level', '10'); 

As you can see, if you’re ever locked out of your WordPress website, you at least have the option of running some simple SQL to get back in.