19

Recently I had a client come to me to build a new website. They had also forgotten all their login details, but they did have FTP access.

How do you create an admin user programmatically in WordPress?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Drew Baker
  • 14,154
  • 15
  • 58
  • 97

4 Answers4

39

This will create an admin user if placed in a themes functions.php file. Please change the first three variables as needed.

/* 
* Create an admin user silently
*/

add_action('init', 'xyz1234_my_custom_add_user');

function xyz1234_my_custom_add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    if (username_exists($username) == null && email_exists($email) == false) {

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

        // Get current user object
        $user = get_user_by('id', $user_id);

        // Remove role
        $user->remove_role('subscriber');

        // Add role
        $user->add_role('administrator');
    }
}
SunnyRed
  • 3,525
  • 4
  • 36
  • 57
Drew Baker
  • 14,154
  • 15
  • 58
  • 97
28

Accepted answer has issues and will throw a fatal error when its run twice, because the $user_id will be empty the second time. A work around for the issue:

function rndprfx_add_user() {
    $username = 'username123';
    $password = 'azerty321';
    $email = 'example@example.com';

    if (username_exists($username) == null && email_exists($email) == false) {
        $user_id = wp_create_user( $username, $password, $email );
        $user = get_user_by( 'id', $user_id );
        $user->remove_role( 'subscriber' );
        $user->add_role( 'administrator' );
    }
}
add_action('init', 'rndprfx_add_user');
Blackbam
  • 17,496
  • 26
  • 97
  • 150
user2019515
  • 4,495
  • 1
  • 29
  • 42
5

Here is the queries to create a new admin user :

INSERT INTO wp_users (user_login, user_pass, user_nicename, user_email, user_status) VALUES ('newadmin', MD5('pass123'), 'firstname lastname', 'email@example.com', '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');
TylerH
  • 20,799
  • 66
  • 75
  • 101
Gaurav Gupta
  • 478
  • 6
  • 10
  • Not recommended to go this route, WordPress has built in functions to handle user creation. – Chris Oct 22 '19 at 08:29
  • 2
    Yes wordpress have but sometimes if we dont have login credentials and we are working for a client. So in that case we needs to make a admin user from backend to login. – Gaurav Gupta Oct 23 '19 at 05:30
1

Simply, you need to add this code sample in your functions.php:

function wpb_admin_account(){
  $user = 'Username';
  $pass = 'Password';
  $email = 'email@domain.com';
  if ( !username_exists( $user )  && !email_exists( $email ) ) {
    $user_id = wp_create_user( $user, $pass, $email );
    $user = new WP_User( $user_id );
    $user->set_role( 'administrator' );
  } 
}
add_action('init','wpb_admin_account');

Do not forget to change Username, Password and Email by your own data. here is more detailed tutorials that might help you. How to create a new WordPress admin user programmatically

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Syklops
  • 11
  • 1