Add a WordPress Admin User Account via PHP

Oct 15, 2020 | Security - Internet, WordPress, and otherwise


Sometimes our WordPress plugin users need to create an Admin user account for their sites. In this tutorial I will share a small PHP code with you that can be used to create a WordPress Administrator user to your site.

Alternatively, you can also create a WordPress admin user via MySQL.

You will need to have FTP access to your site so you can access and edit the theme files.

Step 1) Log in via FTP

Log into your site via FTP and browse to your theme’s folder. It should be in the following location:

wp-content/themes/

You can use a free software like FileZilla to log into your site using FTP.

Step 2) Add the PHP Code

Copy and paste the following PHP code in your theme’s functions.php file. Change the value of the username, password and email fields.

function create_new_admin_user_account() { $username = 'new_admin'; $password = 'New_Password!123'; $email = '[email protected]'; //Check to see if an account exists with the given username and email address value. if ( !username_exists($username) && !email_exists($email) ) { //All clear. Go ahead. //Create WP User entry $user_id = wp_create_user($username, $password, $email); //WP User object $wp_user = new WP_User($user_id); //Set the role of this user to administrator. $wp_user->set_role('administrator'); }
}
//Run the function with WordPress's "init" hook is triggered.
add_action('init', 'create_new_admin_user_account');

Step 3) Execute the Code

Access your site’s homepage to execute this code.

You can now log in using this newly created user.

Delete the code from the theme’s functions.php file after the task is done.



Source

We’re listening.

Have something to say about this article? Share it with us on Facebook, Twitter or LinkedIn:

SHARE IT HERE:

Subscribe ToThe Weekly SEO Trade News Updates

Get the latest SEO, SEM and SMM marketing intel, tips and tricks from one of the best SEO Gurus online. 

Every Tuesday morning we send out an aggregated email listing all new posts on SEO Trade News.

Excellent! Now check your email to confirm your subscription.