Create new WordPress admin user over FTP

Sometimes you need to create a new admin user without access to the dashboard. We know how to do it via SQL database, and here is how to do it via FTP.

Add new admin in WordPress over FTP by editing function.php

All you will need is just ordinary FTP access. Locate your functions.php file (the path to the file is wp-content/themes/yourtheme). Open the functions.php file in any text editor and paste the following PHP code snippet:

// Note: Remove following snippet after new user had been created!!!
function new_admin_account() {
    $user = 'NewUsername';
    $pass = 'NewPassword';
    $email = 'name@domain.tld';
    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', 'new_admin_account');

Save the changes in the text editor, upload it by overwriting the existing document, and go to the login page using the new admin login details.

As noted in the code snippet, after creating the new admin user, delete the snippet.

Key Points

  • Security Reminder: Ensure you remove this code snippet from functions.php after the new admin user is created to prevent unauthorized access.
  • Functionality: The code checks if the username and email do not already exist before creating a new user and assigning the administrator role.
  • Compatibility: This method is compatible with the latest versions of WordPress.