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.
Leave a Reply