Manually create an admin user in your WordPress site

In

Sometimes WordPress admins lost access to their site dashboard which is very frustrating. Then you have to take an alternative step to gain access to your WordPress dashboard. It happens that you forgotten passwords or installled a malfunctioning plugins, or there could be other technical issues that can lock you out of your WordPress site. This can disrupt your natural workflow and might potentially affect your website’s functionality and business.

There are 3 ways we can manually create an admin user in a WordPress sites. Let’s dive in details.

1. Create a new WordPress admin user in the database

You can simply insert a database entry to your site to create an admin user. To do this,

  1. Log into your phpMyAdmin area or something similar for database management in your hosting control panel.
  2. Find the right database installation.
  3. Under _users table
  4. Click the Insert tab
  5. Let’s fill in the following fields with
    • ID is any number you choose
    • user_login is the username you wish to to access the WordPress Dashboard.
    • user_pass is the password for the user. Make sure to select MD5 in the functions menu
    • user_nicename is the nickname for the user
    • user_email is the email address you want to associate with this user
    • user_registered is the date and time for when this user is registered
    • user_status should be set to 0
    • display_name is the name that will be displayed for this user on your site
  6. Click the Go button
  7. Now under the _usermeta table
  8. Click the Insert tab
  9. Fill in the following fields:
    • user_id is the ID you entered in the previous step
    • meta_key should be the phrase wp_capabilities
    • meta_value should be a:1:{s:13:"administrator";s:1:"1";}
  10. Click Go
  11. Click the Insert tab again
  12. Enter the following information:
    • user_id is the same number you entered in the previous step
    • meta_key should be the phrase wp_user_level
    • meta_value should be the number 10
  13. Click the Go button.

Now you are able to log in with your new WordPress admin user.

2. Add a WordPress Admin User to the Database via PHP

As a developer, you will occasionally need to create a new administrative user in the database to gain access to the site. Typically, this is necessary when you are provided with an export of a WordPress database, but you aren’t provided with the login credentials for the admin user.

Sure, you can gain access via the database. However, many developers aren’t very comfortable with MySQL and the process is pretty slow compared to what I’m about to show you. Here’s all you need to do:

  • Create an mu-plugins/ directory in your site’s wp-content/ directory.
  • Create a new file in the wp-content/mu-plugins/ directory you created and name it anything you want. Something like create-admin-user.php would work nicely.
  • Copy this code snippet and paste it into the file you just created:
<?php

add_action( 'init', function () {
  
	$username = 'admin';
	$password = 'password';
	$email_address = 'webmaster@mydomain.com';

	if ( ! username_exists( $username ) ) {
		$user_id = wp_create_user( $username, $password, $email_address );
		$user = new WP_User( $user_id );
		$user->set_role( 'administrator' );
	}
	
} );
  • Edit the username, password and email address.
  • Login to the site using the information you just edited.
  • Once you have logged in, delete the file.

3. Create WordPress Admin user with WP-CLI

If you are familiar with super tool WP-CLI then you can create an admin straight from your terminal in just a single command.

wp user create marquesas marquesas@mail.com --role=administrator

After the user is created the WP-CLI tool will provide you the password to login it with.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *