skip to content
Ruban Selvarajah

Locked Out of WordPress? Add an Admin User via the Database

/ 2 min read

Ever found yourself locked out of your WordPress admin account? Or maybe you need to create a new admin user for your site, but you can’t do it the traditional way. Whatever the reason, knowing how to add an admin user directly through the WordPress database can be a lifesaver.

Let’s 🚀

Getting Started

Generate a Hashed Password

First things first, you need a hashed password for the new user. WordPress uses a specific hashing system, so you can’t just enter a plain text password. Use online tools like WordPress Password Hash Generator or WPRefers to generate one.

Accessing the Database

Assuming you have access to your website’s database (usually through phpMyAdmin or a similar database management tool), here’s what you need to do:

Insert User Data

Navigate to your WordPress database, often named wp or something similar. Then, run the following SQL query to create a new user. Replace the values with your desired username, email, and the hashed password you generated. I’m using $P$BY1w0mqOCkzKHikYeyaoCKZuZqckhL0 which is the encrypted value for password ad running the query on a MySQL database.

use my_wp_database;
insert into wp_users (user_login, user_pass, user_nicename, user_email, user_registered, user_status, display_name) values ('neo', '$P$BY1w0mqOCkzKHikYeyaoCKZuZqckhL0', 'neo', 'neo@example.com', NOW(), 0, 'neo' );

Set Admin Privileges

Next, you need to give this user admin privileges. This is done by inserting data into the wp_usermeta table.

SET @userId = (SELECT id FROM wp_users WHERE user_login = 'neo');
insert into wp_usermeta (user_id, meta_key, meta_value) values (@userId, 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
insert into wp_usermeta (user_id, meta_key, meta_value) values (@userId, 'wp_user_level', '10');

And there you have it! You’ve successfully added a new admin user to your WordPress site directly through the database. This method is particularly useful in emergency situations, like when you’re locked out of your admin account.

Remember, with great power comes great responsibility. Only use this method when necessary and always ensure your WordPress site’s security is up to date.

Looking for more tips and tricks? Check out my previous posts on Dealing With Hung Terminal Sessions and Debugging Timeout Issues in Web Applications. Stay savvy! 🚀