Creating a new admin user directly via the WordPress database can be useful if you’re locked out of your admin panel or if you need to add a user without access to the WordPress dashboard. Here’s a step-by-step guide on how to do it safely.
How to Create a New Admin User via Database in WordPress
Step 1: Access Your Database
-
Use phpMyAdmin or any database management tool provided by your hosting.
-
Select your WordPress database.
Step 2: Insert New User into wp_users
Table
Run this SQL query to add a new user:
-
Replace
'newadmin'
with your desired username. -
Replace
'StrongPassword123'
with a secure password. -
Replace
'[email protected]'
with the user’s email.
Note: By default, WordPress uses salted hashes, but using
MD5()
here works for immediate login; WordPress will update the password hash on first login.
Step 3: Get the ID of the New User
After insertion, note the ID
of the newly created user from the wp_users
table. If you’re using phpMyAdmin, it will show the last inserted ID.
Step 4: Add User Capabilities in wp_usermeta
Table
Run these two queries to give admin capabilities:
-
Replace
LAST_INSERT_ID()
with the actual user ID if your tool doesn’t support it. -
Make sure the
meta_key
prefix (wp_
) matches your database table prefix (e.g.,wp_
,wp123_
).
Step 5: Log in to WordPress
-
Go to your WordPress login page.
-
Log in with the new username and password.
Important Tips
-
Always backup your database before making direct changes.
-
Adjust table prefixes (
wp_
) if you use a custom prefix. -
Use a strong, unique password.
-
After logging in, update the password from the dashboard for proper hashing.
Here’s a ready-to-run SQL script template you can use to create a new admin user via the database in WordPress. Just replace the placeholders to fit your setup:
How to use:
-
Open phpMyAdmin or your database tool.
-
Select your WordPress database.
-
Paste the above SQL script into the SQL query box.
-
Update the
wp_
prefix if your tables use a different prefix. -
Change the username, password, email, and display name as needed.
-
Run the query.