Deprecated: Function seems_utf8 is deprecated since version 6.9.0! Use wp_is_valid_utf8() instead. in /home/dplugins-code/htdocs/code.dplugins.com/wp-includes/functions.php on line 6131

Deprecated: Function seems_utf8 is deprecated since version 6.9.0! Use wp_is_valid_utf8() instead. in /home/dplugins-code/htdocs/code.dplugins.com/wp-includes/functions.php on line 6131
Create admin user programmatically – Code DPlugins

Create admin user programmatically

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 = "test@dplugins.com"; // 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
Click to Copy