WordPress has six predefined roles: Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. Each role is allowed to perform a set of tasks called Capabilities. That means capabilities for each type of role is different. For example, Subscriber user role has only read capability whereas Contributor has different capabilities, such as read, delete posts and edit posts.
To learn about WordPress user roles, you can read this article.
In this article, We are going to discuss why custom user role is necessary and how to add a custom user role.
Creating Custom User Roles on Your WordPress Site
Suppose, you want to have a user who can only edit posts and read, but among the six default user roles, you will not find a single one who has only read and edit posts capabilities but more or less. In this case, what’s the solution?
You may add edit posts capability for the subscriber by using add_cap(); function, but this is not a good practice. The best idea is to create a custom user role by assigning with capabilities.
There are two major ways of creating custom user roles in WordPress. You can do it by adding custom code or by using a plugin. We will discuss both of the approaches in this post. Let’s see how they work.
Plugin Method: Using A WordPress Custom User Role Plugin
You may like to use plugin instead of adding custom code. To create new user roles you can use Members plugin, it is a very popular user and role management plugin that was created to make WordPress a more powerful CMS.
Creating a New User Role
- After installing and activating this plugin navigate to wp-admin → Users → Add New Role
- Enter the role title (e.g Comments Moderator)
- Select the capabilities for this new user role (e.g. moderate comments, read)
- Click on Add Role button.
Now, go to wp-admin → Settings → General, click on New User Default Role dropdown field. You can see the newly created user role listed along with the default user roles.
It's very easy, right? Using this handy plugin, we have created a WordPress custom user role and it took only a few steps!
Custom Code Method
WordPress is the number one CMS in the world and it is popular for its open source nature. This means you have the opportunity of customizing WordPress the way you want and its completely FREE. Just like that WordPress gives you the opportunity to create custom user role by using a function called add_role();
There are three parameters in
add_role( $role, $display_name, $capabilities );
- $role (string) (required): Unique name of the role
- $display_name (string) (required): The name to be displayed
- $capabilities (array) (optional): Capabilities that one can access
Let’s create a new user role named Custom Editor with read and edit posts capabilities. Include the following lines of code toward the end of your functions.php, which is located in your theme folder.
add_role( 'custom_editor', __( 'Custom Editor' ), array( 'read' => true, // true allows this capability 'edit_posts' => true, ) );
Now save the file, login to your site with an admin account. Navigate to Settings → General. You can see the newly created user role in the user list.
That’s all, you can assign a user to this role from WordPress admin panel, also you can set this role as New User Default Role.
We hope this tutorial will be very helpful for beginner users. If you have any suggestions or query, please let us know in the comments.