Blog

Adding an Admin User to WordPress Using MySQL and phpMyAdmin

Use MySQL and phpMyAdmin to add an admin user to WP

There may be situations where you find yourself locked out of your WordPress Admin Dashboard. This can happen if your website gets hacked or if a WordPress update fails to complete properly.

Fortunately, there is a solution that allows you to create a new WordPress Admin user using the phpMyAdmin tool, which is typically found within your hosting cPanel account. This guide will walk you through the steps to access and edit your WordPress MySQL database tables.

Step 1: Accessing the Database

  1. Log in to your hosting cPanel account.
  2. Look for the phpMyAdmin tool and click on it to open it.

Step 2: Selecting the Database and Navigating to the SQL Tab

  1. Once inside phpMyAdmin, locate and select your WordPress database from the list of databases on the left-hand side.
  2. After selecting the database, you should see a navigation menu at the top of the page. Click on the "SQL" tab.

Step 3: Adding the New Admin User

  1. In the SQL tab, you will see a text area where you can enter SQL commands.
  2. Copy and paste the following SQL query into the text area:
    SQL
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`,
`user_status`) VALUES ('newadmin', MD5('password'), 'New Admin', 'admin@example.com', '0');

Note: Replace 'newadmin' with your desired username and 'password' with your preferred password.

  1. Click on the "Go" button to execute the SQL query.

Step 4: Granting Admin Privileges to the New User

  1. After executing the SQL query, locate and select the wp_usermeta table from the left-hand side.
  2. In the search box, enter the following criteria:
    • Field: user_id
    • Operator: =
    • Value: ID, where ID represents the ID of the newly created admin user (typically the next available number).
  3. Click on the "Go" button to perform the search.
  4. Find the row where the meta_key column is set as wp_capabilities and click on the "Edit" button.
  5. In the meta_value field, update the value to the following:
REPLACE INTO `wp_usermeta`
SET `meta_value` = 'a:1:{s:13:"administrator";b:1;}'
WHERE `user_id` = ID AND `meta_key` = 'wp_capabilities';

Step 5: Finalizing the Process

  1. Once you have made the necessary changes, click on the "Go" button to save them.
  2. You can now log in to your WordPress Admin Dashboard using the newly created admin username and password.

By following these steps and utilizing phpMyAdmin, you can add a new admin user to your WordPress website's database, regaining access to your WP Admin Dashboard. Remember to choose a strong password for enhanced security.

Alternative when you have no access to phpMyAdmin

If you do not have access to phpMyAdmin you can still create a new admin user by uploading the following PHP script.

<?php
// Include the WordPress configuration file
require_once('./wp-config.php');

// Include the WordPress core file
require_once('./wp-load.php');

// Create a new admin user
$username = 'newadmin';
$password = 'password123';
$email = 'admin@example.com';

// Check if the username is available
if (username_exists($username)) {
    echo "Username already exists. Please choose a different username.";
    exit;
}

// Check if the email address is already registered
if (email_exists($email)) {
    echo "Email address is already registered. Please choose a different email.";
    exit;
}

// Create the new admin user
$user_id = wp_create_user($username, $password, $email);

if (is_wp_error($user_id)) {
    echo "Error creating user: " . $user_id->get_error_message();
    exit;
}

// Set the new user as an administrator
$user = new WP_User($user_id);
$user->set_role('administrator');

echo "New admin user created successfully!";
?>

Upload this script to the same directory as wp-config.php, and load it from the browser by browsing to the filename you uploaded.

Make sure to adjust the values of $username, $password, and $email to your desired values for the new admin user.

Remember to remove this script from your server after running it, as leaving it accessible can pose a security risk.

Makis Mourelatos

WC Athens 2016 co-organizer, WP Support and Security Aficionado, Wannabe Kitesurfer.

Share
Published by
Makis Mourelatos

Recent Posts

WordPress Theme Development

WordPress is a widely popular Content Management System (CMS) that powers over 40% of all…

2 years ago

WordPress Solutions

WordPress is a popular platform that empowers more than 60 million websites worldwide. Millions of…

2 years ago

WordPress Site Maintenance

In this article, we will cover ten crucial WordPress site maintenance tasks that every website…

2 years ago

WordPress Security Solutions

In this blog article, we will explore the various WordPress security solutions you can implement…

2 years ago

WordPress Plugin Solutions

Plugins are an integral part of WordPress, as they offer countless benefits and features that…

2 years ago

WordPress Optimization and Performance Improvement

In this article, we will explore various strategies that can help you enhance your WordPress…

2 years ago