WordPress Create User and Add Role, Lost Password Cant Login

If you cannot log in to your site, you cant reset your password and you have your FTP connection this may be a good solution for you.

Just add this code to your themes functions.php and reload your website ones. This will create an administrator role user.

$userdata = array(
    'user_login' =>  'username',
    'user_mail'   =>  '[email protected]',
    'user_pass'  =>  'passwords123' 
);
 
$user_id = wp_insert_user( $userdata ) ;
 
// On success.
if ( ! is_wp_error( $user_id ) ) {
    echo "User created and the id is: ". $user_id;
     
     $user_id_role = new WP_User($user_id);
     $user_id_role->set_role('administrator');

}

 

You will see a “User created and the id is” result on top.

If you have a cache just go into the wp-login.php page and reload the ones there. That will bypass the cache.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments