Use MySQL and phpMyAdmin to add an admin user to WP
There may be situations where you find yourself locked out of your WordPress Admin Dashboard. This can happen if your website gets hacked or if a WordPress update fails to complete properly.
Fortunately, there is a solution that allows you to create a new WordPress Admin user using the phpMyAdmin tool, which is typically found within your hosting cPanel account. This guide will walk you through the steps to access and edit your WordPress MySQL database tables.
Step 1: Accessing the Database
- Log in to your hosting cPanel account (or your host’s control panel).
- Look for the phpMyAdmin tool and click on it to open it.
Step 2: Selecting the Database and Navigating to the SQL Tab
- Once inside phpMyAdmin, locate and select your WordPress database from the list of databases on the left-hand side.
- After selecting the database, you should see a navigation menu at the top of the page. Click on the “SQL” tab.
Step 3: Adding the New Admin User
- In the SQL tab, you will see a text area where you can enter SQL commands.
- Copy and paste the following SQL query into the text area:
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`,
`user_status`) VALUES ('newadmin', MD5('password'), 'New Admin', 'admin@example.com', '0');
Note: Replace 'newadmin' with your desired username, 'password' with your preferred password and admin@example.com with your email address.
- Click on the “Go” button to execute the SQL query.
Still locked out of WordPress? We can get you back into your dashboard and find out what caused it. Flat price, usually the same day. Get your price →
Step 4: Granting Admin Privileges to the New User
- After executing the SQL query, select the
wp_userstable from the left-hand side and click “Browse”. Note theIDof the newly created admin user (typically the highest number in the list). - Go back to the SQL tab and run the following query, replacing
2with that ID:
INSERT INTO `wp_usermeta` (`user_id`, `meta_key`, `meta_value`)
VALUES (2, 'wp_capabilities', 'a:1:{s:13:"administrator";b:1;}'),
(2, 'wp_user_level', '10');
This adds two rows to the wp_usermeta table: wp_capabilities, which gives the user the administrator role, and wp_user_level.
Tip: To change an existing user to admin instead, skip Step 3, find that user’s ID in wp_users and run this query:
UPDATE `wp_usermeta`
SET `meta_value` = 'a:1:{s:13:"administrator";b:1;}'
WHERE `user_id` = ID AND `meta_key` = 'wp_capabilities';
Step 5: Finalizing the Process
- Once the query has run, phpMyAdmin will confirm how many rows were added or changed.
- You can now log in to your WordPress Admin Dashboard using the newly created admin username and password.
By following these steps and using phpMyAdmin, you can add a new admin user to your WordPress website’s database, regaining access to your WP Admin Dashboard. Remember to choose a strong password for enhanced security.
We had a new client who couldn’t access their WordPress dashboard and all of their blog posts were missing. FixMyWP quickly solved the login issue and repaired the WordPress database. The site was up and running again, preventing further problems for us and for the client.
Alternative when you have no access to phpMyAdmin
If you do not have access to phpMyAdmin, you can still create a new admin user by uploading the following PHP script.
get_error_message();
exit;
}
// Set the new user as an administrator
$user = new WP_User($user_id);
$user->set_role('administrator');
echo "New admin user created successfully!";
?>
Upload this script to the same directory as wp-config.php, then open it in your browser by going to the file name you uploaded (for example, yoursite.com/new-admin.php).
Make sure to adjust the values of $username, $password, and $email to your desired values for the new admin user.
Remember to remove this script from your server after running it, as leaving it accessible can pose a security risk.
Need a hand with your WordPress site?
Answer four quick questions and see your flat price before you pay. Most fixes are done the same day or within 24 business hours.
