If you’re working on a WordPress website that doesn’t have an administrator login, or the client/site owner has forgotten all their login details, then you can quickly create a new Admin user with only FTP access.

How to Create a New WordPress User with PHP

Use the following steps to create a new administrator level WordPress user programmatically.

Step 1: Connect with FTP

Connect to the WordPress site’s file system using SFTP/FTP and open up the functions.php file of the active theme. If you’re not sure what the active theme is you can view the source in your browser and search for text matching /wp-content/themes/. The folder after themes/ is the active theme for the site.

Step 2: Add Functions to Create Admin Users

Add the following PHP code to the theme’s functions.php file:

/**
 * Programmatically Create Admin User in WordPress
 */
function kevinlearynet_add_user() {

  // Only when ?create-admin-user is in the URL
  $create_admin_user = isset( $_GET['create-admin-user'] );
  if ( !$create_admin_user ) {
    return;
  }

  // Change these values to whatever you want
  $username = 'adminuser';
  $password = 'password';
  $email = '[email protected]';
  $login_url = wp_login_url();

  // No user exists
  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' );
    wp_die( "Administrator user added for $email.<br><br><strong><a href='$login_url'>Login Now</a></strong>" );
  }

  // User already exists
  else {
    $user = get_user_by( 'email', $email );
    wp_set_password( $password, $user->ID );
    wp_die( "Administrator user already exists for $email, but we've updated the password for you.<br><br><strong><a href='$login_url'>Login Now</a></strong>" );
  }
}
add_action('init', 'kevinlearynet_add_user');

Step 3: Create The Admin User

Now that the code is in place you can visit the site at https://www.yourdomain.com/?create-admin-user to add the user to the system.

Step 4: Remove Functions

Once you’re done it’s very important to remove the functions that we added to the theme’s functions.php file for security reasons, this should really only be implemented once to gain access and then must be removed.

Summary

You should now have access to your WordPress site as an Administrator. This is a handy tool to have at your disposal when working with new clients as a freelance WordPress developer. It’s often easier to use than other common methods, and it avoids a common blocking point when working with a new WordPress website.

Meet the Author

Kevin Leary, Boston WordPress Developer of 10 Years

I'm a freelance web developer and WordPress consultant in Boston, MA with 13 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.