However WordPress custom user roles have been a popular choice for websites that require a content management system. Because just like other CMS, WordPress offers comprehensive predefined user roles that meet the needs of website administrators and managers. But the owners of any WordPress hosted website do not know that they can limit access to the dashboard for a particular user by creating new custom user roles. Because This action ensures that only a specific group of users has access to specific admin panel areas and options. Then Custom user roles help reduce the likelihood of any casualties that can bring down the entire website.
For example In this article, I will describe two ways to create new custom user roles using the WordPress User Interface Editor and will ensure that everything on your website is under control.
Understanding WordPress User Roles
In the same way WordPress, the user is a combination of role:
However a role is the name of a user group that will be displayed in your WordPress Admin Panel and the capabilities are the privileges that administrators can enable or disable.
Also By default, WordPress has 6 basic user roles.
- Super Admin: Generally the profile that has access to the full website, including network administrative features.
- Administrator: Profile containing all administrative privileges.
- Editor: A profile that can create, edit, publish, and post the posts of themselves and other users.
- Author: A profile that can create, edit, edit only your posts.
- Contributor: A profile that can create, edit, and not publish their own posts.
- Subscriber: Profile (profiles) that can only manage their own profiles.
Then You can find current user roles with your WordPress Admin dashboard.
Then Sign in to your WordPress Admin Panel, navigate to User → All Users.
Also, You can see the current user roles available on your WordPress website:
Be ready to create new user roles
Generally, there are two types of ways to create, edit, or delete user roles in WordPress.
1. Modify WordPress user roles through the plugin
As well as Sometimes a plugin that can control user roles in WordPress provides the simplest solution. Because there are many plugins to add, modify, and delete user roles and capabilities. Also I would suggest that the user role editor plugin.
Once installed and activated, navigate to User → User Role Editor from the left panel.
Finally, You can see, there are more options to add/modify/delete.
How to Add Custom User Roles in WordPress
Additionally, First of all, note that you can see all the user roles on your WordPress site. If you want to update or change anything, choose the user’s role from the list.
In my case, I have selected and marked the role of the editor where it has been said that only you can see the current capabilities handed over to the editor.
If you are unfamiliar with the capabilities of a role, then this extension WordPress codex To understand from them, read them.
The user role editor allows you to rename capabilities by checking the box.
And here’s how the names of abilities will be changed in the human-readable form.
Ok so now you have got an idea of how the plugin lists the existing capabilities for each WordPress user’s role.
To provide more power for the user role, you can go to the related group on the left side. For example, in addition to the current capabilities, I want to allow the add editor to add and activate the plugin
For this, go to Plugins Group from the left panel and select Install plugins and Activate plugins and clicking Update. Update on the right button.
Usually Now go to the Granted Only section and you can see that editors can now install and activate the plugin.
To add (or remove) capabilities for a particular user role, navigate to the group related to the left panel and assign capabilities (or extract) accordingly.
Create a new WordPress user role
User role editor allows you to create new roles in addition to administrators, writers, editors, etc. Also Click the Add Role button from the right menu, and a new popup will ask you:
- Role Name (ID): A unique ID for each user’s role.
- Display role name: The user name of the role that will be displayed.
- Copy: Select the existing role that will form the basis of the new role.
In my case, I named copy the editor role and the new role editor limited access. As you can see, the new role has the same capabilities as the original editor role.
Now, I can add/remove capabilities for this new user role.
Like adding a role, you can also add/create new capabilities and assign them the role of any WordPress user.
Next, I’ll display how you can create a custom WordPress user role via code.
2. Create, edit, or delete user roles manually in WordPress
WordPress allows you to create custom user roles by removing default user roles and specifying limited privileges/capabilities to specific user groups (roles).
WordPress provides five tasks for management of roles and capabilities:
- add_role () : To add a custom role.
- remove_role () : To delete the custom role.
- add_cap () : To add a custom ability to a role.
- remove_cap () : To delete a custom ability from a role.
- get_role () : Receives information about the role and its capabilities.
Remove default user roles
I will start by removing the existing roles. Remember that WordPress has the following five roles by default:
I will remove all user roles except the Administrator in WordPress. For this, navigate to WordPress Admin → Appearance → Editor → Theme Function.
I will use remove_role() to remove the role. Copy the following code snippet and paste it at the end of the Theme Functions file(function.php).
remove_role( 'subscriber' ); remove_role( 'editor' ); remove_role( 'contributor' ); remove_role( 'author' );
Click the Update File button.
Finally To verify that user roles have been removed, navigate to user → all users.
Then You can see that all default user roles have been omitted, except admin.
Create new user roles
I will create three new user roles in WordPress.
- Administrators: With full administrative access.
- Moderator: You can create, edit, publish, and post your own and other users.
- Newbie: who can only edit their profile and create new posts
To add these custom user roles, I will use the original add_role () function with the following syntax:
add_role( $role, $display_name, $capabilities );
- $ role: a unique name for the role
- $ display_name: The name that appears in the WordPress Admin Panel
- Capabilities: Role Privilege
The complete list of all the capabilities can be found here.
Since I have not removed the default administrator role, therefore all the capabilities and privileges of the role are intact.
This role has the right to create, edit, and publish posts from us and other users.
Copy the following code and paste it at the end of the Theme Functions file (function.php).
add_role('moderator', __( 'Moderator'), array( 'read' => true, // user to read 'create_posts' => true, // create new posts 'edit_posts' => true, // user to edit their own posts 'edit_others_posts' => true, // user to edit others posts too 'publish_posts' => true, // user to publish posts 'manage_categories' => true, // user to manage post categories ) );
Click Update File.
Assign a “moderator” role to a user
Finally, I will assign this user role to a user. For this, go to WordPress Dashboard → Users → All Users and follow the instructions below.
- I assigned the user moderator roll “ Alex “. You can see (from the image below) that when he logs in and goes to “ dashboard “, he will have limited access according to the capabilities of his new role.
- newbie This role can only edit their profile and create new posts.To add this user role, copy the following code and paste it at the end of the Theme Functions file.
add_role('newbie', __( 'Newbie'), array( 'read' => true, // Allows a user to read 'create_posts' => true, // Allows user to create new posts 'edit_posts' => true, // Allows user to edit their own posts ) );
After pasting the code, click the Update File button.
Assign a “newbie” role to the user
Then assign a new role to a user, go to WordPress Dashboard → Users → All Users and follow the instructions below.
- I assigned the user the role of Newbie “ Dicaprio “. You can see (from the image below) that when he logs in and goes to Dashbor D, he has limited privileges.
Now, I will assign the role of Newbie to each visitor registering on my website.
- Now, I will assign the role of Newbie to each visitor registering on my website.