Create an admin user in WordPress

This is Q & A:

I recently came to a client to create a new site. They also forgot all their login details, but they had FTP access.

So how do you create an admin user using code?

+8
wordpress admin
source share
3 answers

This will create the admin user if he is placed in the functions.php theme file.

Modify the first three variables as necessary.

/* * Create an admin user silently */ add_action('init', 'add_user'); function add_user() { $username = 'username123'; $password = 'pasword123'; $email = 'drew@example.com'; // 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' ); } 
+13
source share

The accepted answer has problems and will generate a fatal error if run twice, because $user_id will be empty a second time. Work on the problem:

 add_action('init', 'prefix_add_user'); function prefix_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' ); } } 
+9
source share

The following are the requests 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'); 

This will definitely help you. :)

0
source share

All Articles