How to Add a WordPress Administrator User through PHP

Occasionally, there is a need to provide a way to add users to the WordPress Administrator without using its interface. This method can be used to add users of any role that you have defined within WordPress. Once you add the function, use it only to add a user and then remove the code when it is complete. Follow the directions below:

Adding a WordPress User through the Theme Functions.php File

  1. You will need to have administrator access to the installation files for your WordPress installation. You can use FTP or a file manager like the one available in cPanel. There are multiple options depending on the system used by your host. However you access your files, use that option now and navigate to the location where you have installed your WordPress files.
  2. Once you see the files for your WordPress installation, open the WP-CONTENT folder and then open the THEMES folder underneath it.
  3. Once you are in the Themes folder, locate the folder for the theme that you are using with your WordPress installation. Open that theme folder.
  4. Search for functions.php file, then open it in an editor so that you can edit it.
  5. Copy and paste the following function into the Functions.php file:

     function admin_account(){ $user = 'Username'; $pass = 'Password'; $email = '[email protected]'; 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','admin_account'); 

    Make sure that you change username, password, and email to something unique or this function will not work. The username, password and email that you use are the criteria applied to the user. This function has been tested with WordPress version 4.9.7.

  6. Save changes.

Verifying the User Addition

When you have saved the file, simply reload your website and then login to the WordPress Administrator. Verify that your user has been added. Remember that when you have completed adding the user that you should remove the added code from your functions.php file.

AC
Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X