Cartoon style illustration of Addy, young man with long blonde hair in a blue hoodie
addy.zone
digital garden 🌱

Create a WordPress admin account via functions.php

If you’re locked out a WordPress website (eg. forgot admin login/reset password not working/malfunctioning security plugin) but have access to FTP, you can easily create a new admin account right from your themes functions.php.

function addy_create_admin_account() {
    // Replace with your desired credentials
    // Both user & email must not already be in use
    $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','new_admin_account');

This function accepts a username, password and email that you can pick. As long as that username or email does not already exist in the users table, it will create the user and set the role of that new account to administrator.

Be sure to remove this snippet as soon as the account is created, else it will attempt to run every time.

Leave a Reply

Your email address will not be published. Required fields are marked *

three + 4 =