Ever needed to hack your way into a WordPress through the database?
Sometimes it happens that you lock ourselves out of the WordPress admin dashboard because you either made changes unexpectedly or lost access somehow due to some other issue. I’ll show you how to get admin access by editing admin users in the database, or creating a new admin user in the database.
Follow this simple guide…
To edit your database, you can use any database administration tool. Most webhosts have phpMyAdmin or one that works similarly. You can also use adminer, which is a single file that you can upload via FTP to your website root folder.
METHOD #1 – change password for existing user (EASY)
This method gains access by editing the password for an existing admin user.
- From phpMyAdmin or whichever database administration tool, scroll down to the “wp_users” table. (It might have another prefix, like “123vw_users”.)
- Click “edit” on a user that you know has admin access.
- For the “user_pass” column, select “MD5” for varchar type, and then enter in the desired password in the “Value” field.
- If you see anything in the “Value” box next to the “user_activation_key” column, delete it.
- Click [GO].
You should be able to log in using that username or email, and then the password you entered.
If you have too many users and can’t find (or don’t know) the admin ones, you can search for them below using this SQL command. (You can run it by clicking the [SQL] tab in your MySQL administration tool.)
SELECT u.ID, u.user_login, u.user_nicename, u.user_email
FROM wp_users u
INNER JOIN wp_usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
AND m.meta_value LIKE '%administrator%'
ORDER BY u.user_registered
- If your database uses a custom prefix, make sure you replace all the “wp_” with your prefix.
- After you run this SQL command, it’ll show you the admin users. Then you can go back to the users table, sort by name or email, and edit one of them to gain access.
METHOD #2 – create new user (takes 5 minutes)
This method gains access by adding a new admin user.
Open up phpMyAdmin or whichever MySQL administration tool you have, click “SQL” and paste the code below. Make sure you change the name, username, mail and password which you need to create for the new user…they are marked in BOLD.
Also, if your database is not using the default WordPress prefix _wp
, make sure you also change the prefix of 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
.
Enter this code below…
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`,
`user_status`)
VALUES ('username', MD5('password123'), 'John Doe', '[email protected]', '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');
Click “GO” after you enter it with your specified changes. After executing the function you will be able to access your WordPress dashboard again with that new user credentials.
METHOD #3 – give admin permissions to existing user (10 seconds)
- Go into phpmyadmin and look in the
_usermeta
table. - Scroll to the user you want to alter permissions for.
- Look for the
_capabilities
meta_key and change the meta_value toa:1:{s:13:"administrator";s:1:"1";}
.
geo
Indeed – this is a nice one. Had to use it the other day 🙂
Alan
You can also create a user with just code, useful if for some reason you only have lets say FTP access.
e.g. https://gist.github.com/alanef/08ad974f07110323244ad20317aa4fa3
Johnny
I love it, Alan. Thank you for this! Do you mind if I make a separate post sharing this code and giving credit to you? Let me know where you’d like to link to directly. Perhaps the code page and also your own personal/professional work link?
Scott Dayman
I’ve had to do this before…and then realized it’s so much easier to us WP-CLI. You’ve only mentioned WP-CLI a few times, but I use it *so much* for admin tasks. Its built in syntax help always makes it easy for me to stumble my way through any task, including password changes, role changes, and user creation.