How to Create and Add Admin User With MySQL in WordPress

Did you lock yourself out of the WordPress admin dashboard?
That can happen due to changes in the database, an accident, or a technical issue that deleted your WordPress admin account.

To fix that, you can create and add a new admin account by applying one of these options:

  • Add a piece of little code into a mu-plugin
  • Add the user directly into the database by using an SQL query

Add a WordPress Administrator With a mu-plugin

Add this code into a new mu-plugin or add it into an existing one on top of it:

/*
Plugin Name: Add admin account
Description: This adds an admin account into database
Author: WP STAGING
Version: 1.0
Author URI: https://wp-staging.com
*/

add_action('wp_loaded', function() {
if (isset($_GET['add-admin'])) {
$inserted = wp_insert_user([
'user_pass' => 'password',
'user_login' => 'username',
'user_email' => 'test@example.com',
'role' => 'administrator'
]);
var_dump($inserted);
exit;
}
});

Change the variable’s user_pass, user_login, and user_email to your desired values.

Open your website with the parameter https://example.com/?add-admin

Then you can log in with that new user account.
After that, remove the mu-plugin from your site for security reasons.

Add a WordPress Admin User With a SQL Query

To do so, open your favorite database administration tool to look for the SQL-Query import function.

In that case, I use the popular adminer, a database administration tool that consists of only one file you can upload via FTP to your website root folder. Another popular tool is PhpMyAdmin, which is already installed on most website hosting servers.

Create WordPress Admin User SQL Query

Paste the code below into the SQL Query field of PhpMyAdmin or Adminer and change the name, username, mail, and password you like to use for the new user. (Bold strings).

Important: If you have another table prefix instead of wp_ you’ll need to modify the table prefix as well!

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`,
`user_status`)
VALUES ('wpstaging', MD5('password123'), 'Rene Hermenau', 'support@domain.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');

How to create an admin user with SQL for WordPress
SQL Query

If your website is not using the default WordPress prefix _wp also, change the prefix of the database tables wp_users, wp_usermeta, wp_capabilities and wp_user_level to the one, your website is using.

So if your website is using the table prefix wpstg0_ the new table name for wp_capablities is wpstg0_capabilities.

After executing the function, you can access your WordPress dashboard with that new user credentials again.

I hope this article helped you understand how to manually create a new admin user in your database. If you still have any questions or something is not entirely clear, please contact us.