Whenever you’re not able to access your WordPress wp-admin panel due to the wrong login credentials, either you need to recover the password or create a new account.
If you want to create an administrator account, you can use the MySQL database, but if you wish to add a subscriber, contributor, or an editor, you have to deal with some coding lines.
Writing an SQL query for a specific user role can be tricky. You may be wondering as if what’s the easiest way to accomplish such a task.
Well, if you’re aware of the functions.php file of your WordPress theme, you can easily add a new user with a specific user role.
You may be wondering to edit the file from your WordPress admin panel, but due to security reasons, you may not see the editor.
To remove a vulnerability, WordPress experts suggest disabling the file editing. So, the best option is to use cPanel. I am sure; you know how to do it.
A Step By Step Guide to Add a New User
Before you proceed, you must have a backup of your WordPress website and its database. You should know that functions.php is one of the most important files, which controls all the functions of the site’s layout.
If you add a wrong code, the website layout may get disrupted. That’s why it’s vital to learn the correct way to copy and paste a custom code.
I am sure, you have seen the different layout of cPanel, it’s because every web hosting tries to improvise the default cPanel design to match its brand color.
Follow These Steps
Step 1:
I hope you know that the data of a website resides in the file manager.
In most of the cases, you can see it under the Files’ section. Some web hosting companies offer a different spot.
Step 2:
But you need to open publc_html, navigate to it from the left-side vertical menu.
Note: If you host multiple websites and try to update any other domain than a primary domain, you have to open the folder where WordPress installation is available.
For a single website, the data is available in the pulic_html directory.
Step 3:
Step 4:
You can see tons of folders. Click on Themes.
Step 5:
Right now, you need to edit the active theme, click on its folder to open.
Step 6:
Step 7:
Now, let me show you a code to add a contributor.
functioncontributor_new_account(){ $user = 'Username'; $pass = 'Password'; $email = 'email@yoursite.com'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'contributor' ); } } add_action('init','contributor_new_account');
Note: Don’t forget to replace the username , password, and the email address with the ones you want to use as your login credentials.
If you have noticed, there is a coding line displaying the user role.
$user->set_role( 'contributor' );
You can replace it to set any user role. For a subscriber, the code is as follow.
functioncontributor_new_account(){ $user = 'Username'; $pass = 'Password'; $email = 'email@yoursite.com'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'subscriber' ); } } add_action('init','contributor_new_account');
If you’re wondering about the contributor_new_account() function, it’s just a name, you can change it if you want. But if you have no idea about WordPress codex, please copy and paste the code to your file.
You need to change the user role, nothing else. For example, the code to add an editor is as follow.
functioneditor_new_account(){ $user = 'Username'; $pass = 'Password'; $email = 'email@yoursite.com'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'editor' ); } } add_action('init','editor_new_account');
Depending on your requirement, you can add a user with its role, and click on the Save Changes button to save the file.
Try clearing the browser cache and cookies before logging in to your WordPress website. If you see such a new user, congrats, you have successfully learned to create a new user with a specific role.
Conclusion
Whether you create an administrator or an editor, you can use the code by a little bit of modification. If you understand the concept of WordPress codex, changing the function and the user role is easy.
For a non-techie person, the only requirement is to choose the user role, and save the code. I hope it’s not so hard.
Check out these top 3 WordPress hosting services:
- Do you need the best wordpress hosting? Check out for our recommendations by clicking here.