< Back to Guides

Add WordPress Admin User with SQL

Regaining access to a WordPress website where the Lost Password functionality is failing

Lost the keys to your WordPress website? Locked out and feeling a bit stuck in the mud? It happens to the best of us. Maybe you’ve forgotten your password, or perhaps something’s gone a bit sideways with your usual login. Whatever the reason, sometimes you need a bit of a back door to get yourself back in the driving seat. And if you’re not afraid of getting your hands a little bit technical, using SQL directly can be a surprisingly handy way to create a brand new administrator user.

Now, before we go any further, a little word of caution, eh? Messing about directly with your WordPress database using SQL is a bit like tinkering with the engine of your car. If you don’t know what you’re doing, you could accidentally cause a bit of a bother. So, it’s always a jolly good idea to make a proper backup of your entire WordPress database before you start running any SQL commands. Consider yourself warned!

Right then, assuming you’ve got your safety net in place, here’s the magic incantation – the SQL code you can use to add a fresh administrator account to your WordPress installation:

INSERT INTO wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name)
VALUES (340, 'YOURUSERNAME', 'ANYPASS', 'webdev', '', '', NOW(), '', 0, 'YOURUSERNAME');
INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
VALUES (340, 'wp_capabilities', 'a:1:{s:13:"administrator";b:1;}');
INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
VALUES (340, 'wp_user_level', '10');

Now, let’s have a proper look at what these lines of code are actually doing, shall we?

The first INSERT statement is all about adding a new entry into the wp_users table. This table holds all the information about your website’s users. Let’s break down the bits in the VALUES section:

  • (340, ...): This is the ID for your new user. Now, here’s a crucial bit: you’ll probably want to choose a different number here that isn’t already being used by another user in your wp_users table. You can usually have a peek at the existing IDs in your database to find an unused one. Just make sure it’s a whole number.
  • 'YOURUSERNAME', ...): This is where you’ll want to put the username you want for your new administrator account. Replace 'YOURUSERNAME' with whatever you fancy.
  • 'ANYPASS', ...): This is the password for your new user. Important note: this password will be stored in the database as plain text with this command. Once you’ve logged in with this new account, it’s a really good idea to go into your user profile and change your password to something a bit more secure – WordPress will then properly encrypt it. Replace 'ANYPASS' with a temporary password.
  • 'webdev', ...): This is the user’s “nicename,” which is a slightly more user-friendly version of their login. You can change 'webdev' if you like.
  • '', '', ...): These are for the user’s email address and website URL. You can leave them blank for now if you’re in a hurry.
  • NOW(), ...): This automatically sets the registration date and time for the new user.
  • '', ...): This is for the user activation key (usually used for new user sign-ups). We can leave it blank here.
  • 0, ...): This sets the user’s status to “not spam” and active.
  • 'YOURUSERNAME'): This is the display name that will be shown on the front end of your site. You can make it the same as your username or something different. Remember to replace 'YOURUSERNAME' here as well.

The next two INSERT statements are adding information to the wp_usermeta table. This table stores extra bits of data about your users.

  • The second INSERT adds the wp_capabilities meta key with the value 'a:1:{s:13:"administrator";b:1;}'. This is the magic bit that tells WordPress this user has administrator privileges – they can do pretty much anything on your site.
  • The third INSERT adds the wp_user_level meta key with the value '10'. This is another way WordPress keeps track of user roles, and '10' signifies an administrator.

Right, how do you actually run these commands?

You’ll need to get access to your WordPress database. This is usually done through a database management tool provided by your web hosting company, the most common one being phpMyAdmin. You’ll typically find a link to this in your hosting control panel.

Once you’re in phpMyAdmin (or your database tool), you’ll need to find your WordPress database. It’s usually named something related to your website. Once you’ve selected the correct database, look for a tab or section that allows you to run SQL queries. It might be called “SQL” or something similar.

In this SQL query area, carefully copy and paste the three INSERT statements one after the other. Make sure you’ve replaced 'YOURUSERNAME' and 'ANYPASS' with your desired username and a temporary password, and that you’ve chosen a unique ID (like 340, but ideally one that’s not already in use).

Once you’re sure everything looks correct, hit the “Go” or “Run” button. If all goes well, you should see a confirmation that the queries have been executed successfully.

Now, the moment of truth! Head over to your WordPress login page (yourwebsite.com/wp-admin) and try logging in with the username and the temporary password you just created. If everything’s gone according to plan, you should be looking at your WordPress dashboard as a brand new administrator!

Don’t forget! As soon as you’re logged in, go to the “Users” section in your WordPress admin area, find the new user you just created, and edit their profile to set a strong, secure password. You can also fill in their email address and other details at this point.

So there you have it – a slightly more technical way to get yourself back into your WordPress site if you’ve found yourself locked out. Just remember to tread carefully, back up your database, and choose a secure password once you’re in!