Add Admin User in WordPress By FTP client

Did You Forget WordPress Admin credentials?

 

How will you login now if forget Admin credentials?

 

This happens many times when we forget the login password of WordPress.

If you have FTP details then no need to worry about it. You can create new admin user using FTP.

Just paste this code to your theme’s functions.php.

 

function AddAdminAccount(){
  $user    = 'NewUsername';
  $pass    = 'Password';
  $email   = 'new@email.com';
  $user_id = wp_create_user( $user, $pass, $email );
  $user    = new WP_User( $user_id );
  $user->set_role( 'administrator' );
}
add_action('init','AddAdminAccount');

 

Note: this code can give error if a user already exist with this “user name” and “user email” .

 

And if user not exist it will create new admin user with credential provided by you. but when you refresh page, code will run again , but this time user already exist.

So for removing this fetal error we need to precheck “user name” and “user email” if it already exist or not.

 

Example:

 

function AddAdminAccount(){
  $user    = 'NewUsername';
  $pass    = 'Password';
  $email   = 'new@email.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','AddAdminAccount');

 

Please don’t forget to fill the username email password fields. And don’t forget to delete the code from your functions file after user creation.

 

 

Related Blogs

Upload Any File in My Account Registration Form

To allow users to upload a file in the My Account registration form in WooCommerce, you’ll need to customize the registration form and add a file upload field. We’ll use a custom function and a hook to achieve this. Here’s a step-by-step guide:

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

How To Limit Search To Only Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.