WordPress Custom User Roles can often be very useful.
WordPress, being the most popular CMS of the world, provides a wide range of customizations. One of its useful features is the extension of WordPress User Role.
As you know, WordPress provides 6 predefined user roles that are distributed among individuals based on their significant roles in maintaining the site.
However, most WordPress site owners are not familiar with the fact that they can create custom user roles in addition to the fixed ones provided by WordPress itself.
Custom User Roles actually come in handy. If you wish to give a specific task to someone with less authority, you can create a custom user role for him. Here you can allow him to access your dashboard with limited access and control required to carry out those specific sets of tasks only.
Thus limiting him from interfering with other functionalities of your website, but also confining his tasks according to his specific role and responsibilities.
This is the ‘best practice’ towards amateurs because it reduces the risk of unwanted and unprofessional trespasses to a great extent.
However, in this article, you will get a guide on how to create WordPress Custom User Roles and how to assign them to its users. Let us begin.
User Roles and Capabilities
A User Role is a combination of two terms:
- Role
- Capability
A Role is the name of a single user or a group of users that shows up in the Admin Panel of WordPress. And, Capabilities are the limited privileges that are assigned to that specific group or person. Together, these two make up a user role.
Generally, WordPress has 6 user roles by default:
- Administrator: The Supreme profile that has access to all kind of administrative activities in your WordPress site. When you create a website, WordPress sets your profile as Administrator by default.
- Editor: This user profile can create/edit/publish their own posts as well as others’ posts. They are responsible for managing the overall content of the WordPress site.
- Author: User assigned with this role can create posts, edit those posts, and manage comments in their own posts only.
- Contributor: This role allows the user to create/edit their own post, make a draft of it but can not publish them. Usually, this role indicates that the user is probably an external writer for your blog.
- Subscriber: A subscriber role allows to manage a user’s own profile only. This role allows users to only read or comment on posts.
- Super Admin: A role that is applicable only when you use the WordPress multisite feature. This user role is assigned to manage the network of all sites and it’s users in a multisite network. They can also create/delete or upgrade network and more.
Here is a detailed list of all the capabilities that you can assign to users, according to WordPress Codex.
Creating Custom User Roles In WordPress
There are two ways to create your own custom user roles:
- via Custom Codes
- via Plugins
Here, I will give you a complete walkthrough for both the ways. Therefore, by the end of this article, you will be able to create WordPress custom user roles at ease.
1. Creating WordPress Custom User Roles via Custom Codes
WordPress gives you the upper hand on customization using codes. Hence you can both add new user roles or disable any default user roles easily using codes.
However, For a learning purpose, I will show you how to create a new user role and assign that user with the privileges to read, edit and delete posts.
◈ Creating A Custom User Role
Step1: First, on the Dashboard, go to Appearance > Editor.
Step2: On the right side, under Theme Files, click on Theme Functions.
Step3: The page will show the codes in the functions.php file.
Scroll down to the bottom of the page. In the end, you need to add a code in the following format:
variable = add_role( ‘$role’, __( ‘$display_name’ ), array($capabilities));
In my case, the code looks like this:
$result = add_role( |
// $result is the variable // basic reader is name of the assigned $role // Basic Reader is the assigned $display_name // array is the list of $capabilities allowed for this role // ‘read’ gives user role the capability to read posts // ‘edit_posts’ gives user role the capability to edit posts // ‘delete_posts’ prohibits user role from deleting posts |
** It is highly recommended to use a child theme to apply customizations because any changes you make might not be saved in case developers give an update.
In my case, I have used ‘Twenty Thirteen Child’ theme. In case you forgot to activate your child theme, you can still edit it from here.
On the top right side, under ‘Select Theme to Edit,‘ you can choose your desired child theme and continue editing.
Your functions.php page should look something like this.
After inputting these codes, click on Update at the bottom. And your custom role has been created.
Step 6: Next, assign this custom role to a user.
First, go to User > All Users.
If you wish to assign this role to an existing user, simply chose the user you wish to assign and change the role.
You can also create a new user for this role. Click on ‘Add New‘.
Fill up the information required and choose the role you just created. In my case, it is ‘Basic Reader‘.
Click on ‘Add New User‘.
Thus a new user is ready with the set of capabilities you decided for him.
However, to ensure that you assigned the user properly, Login from the assigned user ID, and check if the user got permission for the correct capabilities.
◈ Edit Capabilities Of A User Role
For an existing User Role, if you wish to add or remove capabilities, you need to follow the previous codes but instead of assigning capabilities in the array, you need to include the following code format:
Add a Capability:
$role = get_role( 'basic_reader' ), |
// ‘basic_reader’ is the name of the $role, not display name // add_cap adds the capability to install plugins |
Remove a Capability:
$role = get_role( 'basic_reader' ), |
// ‘basic_reader’ is the name of the $role, not display name // remove_cap removes ‘read’ capability |
**The ‘get_role’ function fetches the list of capabilities of a user role. In this case, it will fetch the capabilities of the role ‘basic_reader’.
The code will look like this:
$result = add_role( |
Once you update, WordPress will save your changes and the user will have the capabilities added/removed accordingly.
◈ Remove A Default User Role
Let’s say we need to remove ‘Subscriber’ and ‘Author’ role from the user panel.
To do so, you simply need to add the following codes at the end of the functions.php file (same as you added the codes to add a user role):
remove_role( 'subscriber' ); |
// ‘subscriber’ is the $role name // ‘author’ is the $role name |
Then update and the roles will be removed. You can go to Users > All Users and check to confirm that your chosen user role(s) have been deleted.
2. Creating WordPress Custom User Roles via Plugin
WordPress is the largest hub to plugins that provide solutions for almost all sorts of customizations. Therefore, there are plugins with the purpose of user role extension as well.
Some popular ones are WPFront User Role Editor, Members, User Role Editor, and Advanced Access Manager.
It is better to manually create custom roles if needed, but then again, a plugin is also a good option.
Hence, here is an example of how you can create custom user roles using the plugin, User Role Editor.
Step 1: First, go to Plugins > Add New from your dashboard. Search and select ‘User Role Editor’. Install and activate it.
Step 2: Secondly, go to the Users section of your dashboard. You will see a new tab there called “User Role Editor”.
Click on it and the following page will appear:
Here, you will find all the capabilities organized in groups according to their respective purpose. For instance, if you click on ‘Themes’, you will see six capabilities that are related to ‘Themes’.
Step 3: As we said earlier, add role requires three information specifically.
On the right side, click on ‘Add Role‘.
However, it will require two specific information, Role name (ID) and Display Role Name.
The ‘Make Copy of’ option is a special feature of this particular plugin which lets you mirror the assigned capabilities of an existing user role, into the new user role that you are creating. After that, you can add more or reduce capabilities to the new user role.
Step 4: Once you have created the new user role, now you can choose the capabilities you want to assign and click on ‘Update’ on the right side. Thus, your new user role is ready with assigned capabilities.
Step 5: Finally, go back to your All User panel and choose the user you want to assign your new user role to.
Meanwhile, you can also remove an existing user role using the plugin. Click on ‘Delete Role’ on the right side. It will let you choose the role you wish to remove and delete it.
Conclusion
After completing this article, you will be able to create your own custom user roles and assign it to your specific group or a single person.
However just to be on the safe side, it is better to add fewer user roles and assign them to proper users, rather than creating too many roles and randomly engaging users.
No Comments