Create admin user programmatically

Marko Krstić
11 Mar 2021

Sometimes people forget their WordPress admin credentials and email as well so there is no way for them to reset the password to come back in the admin. Following is a little code that you can either add in the functions.php file or set a script in Scripts Organizer and select the PHP option in it.

Code Snippet

<?php

// create admin user programmatically
function dp_create_user()
{
    // pass this as a parameter in your site URL like if your site is dplugins.com then pass it like this dplugins.com/?dp_create_user=yes
    if ($_GET['dp_create_user'] == "yes") {
        $random_password = 'admin123_99'; // password
        $user_email = "[email protected]"; // email
        $user_name = 'dplugins'; // username
        $user_id = wp_create_user($user_name, $random_password, $user_email); // this will create a new user
        wp_update_user([
            'ID' => $user_id,
            'role' => 'administrator', // this will set user role to administrator
        ]);
    }
}
add_action('wp_head', 'dp_create_user', 9999999999);
// create admin user programmatically

?>

Add Code Snippet

Open Scripts Organizer

Hit Add new.

screenshot 2021 04 15 at 21.25.39

Scripts Organizer Settings

Trigger Location: Everywhere
Script Location: PHP

screenshot 2021 04 15 at 21.29.12 1

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy