X
    Categories: WordPress Tutorials, Tips and Guides

How to Add Custom User Roles in WordPress

Reading Time: 5 minutes

WordPress has been a popular option for websites that need a content management system. Like other CMS, WordPress provides comprehensive predefined user roles that fulfill the requirements of almost all users. Many WordPress hosted website owners don’t know that they can limit the access of Dashboard for a particular user by creating new custom user roles. This action ensures that only specific groups of users have access to specific areas. It also helps minimize the chances of any accidents that can bring down the whole website. In this article, I will describe two ways that you use to create new custom user roles using User Role Editor Plugin and ensure that everything on your website is under control.

Understanding User Roles of WordPress:

A User Role is the combination of two things.

  1. Role
  2. Capabilities

Role is the name of a user group that will be displayed in your WordPress Admin Panel and capabilities are the privileges to enable or disable.

By default, WordPress has 6 basic user roles.

  1. Super Admin: Profile that has access to the complete website, including network administrative features.
  2. Administrator: Profile that has all administration features.
  3. Editor: Profile that can create, edit, publish their and other posts too.
  4. Author: Profile that can create, edit, publish their own posts only.
  5. Contributor: Profile that can create, edit their own posts only but cannot publish them.
  6. Subscriber: Profile that can manage their own profiles only.

You can find current user roles within your WordPress Admin Panel.

Login to your “WordPress Admin Panel”.

Navigate to “Users > All Users”.

You can see current user roles like the image below.

Getting Ready to Create New User Roles:

There are two ways to create, edit or delete user roles in WordPress.

1. Plugin To Modify WordPress User Roles:

Sometimes you want to use a plugin that can control User Roles in WordPress. There are lots of Plugins available to add, modify and delete user roles and capabilities. But, I would recommend User Role Editor plugin.

Here is what you can do using this plugin.

But, this article is mainly focused on the manual method.

2. Create, Edit or Delete WordPress User Roles Manually:

WordPress allows you to remove its default user roles and create your own custom user roles by assigning limited privileges to specific user groups.

Today, we’ll teach you how you can remove and create new user roles manually.

There are five functions for managing roles and capabilities:

  • add_role(): To add a custom role.
  • remove_role(): To remove a custom role.
  • add_cap(): To add a custom capability to a role.
  • remove_cap(): To remove a custom capability from a role.
  • get_role (): Gets information about role and its capabilities.

Removing Default User Roles:

First of all we will remove the existing WordPress Default User Roles. In our tutorial we have 5 user roles.

  1. Subscriber
  2. Editor
  3. Contributor
  4. Author
  5. Administrator

Remove all user roles except “Administrator”.

From your WordPress Admin Panel.

Navigate to “Appearance > Editor > Theme functions”.

We’ll use a WordPress function:

  • remove_role(): To remove a role.

Copy” below code and “Paste” it at the end of your “Theme Functions” file.

remove_role( 'subscriber' );
remove_role( 'editor' );
remove_role( 'contributor' );
remove_role( 'author' );

Click on “Update File”.

To verify the deleted user roles.

Navigate to “Users > All Users”.

You can see except administrator all default user roles are deleted in the image below.

Creating User Roles:

In our tutorial, we’ll create three new user roles.

  1. Administrator: That will have complete administrative access.
  2. Moderator: That can create, edit, publish their and other’s post too.
  3. Newbie : That can only edit his profile and create new posts.

To add our own user roles we’ll use a WordPress function.

  • add_role(): To add a custom role.

There are three parameters in add_role function.

add_role( $role, $display_name, $capabilities );
  1. $role: Unique name of the role.
  2. $display_name: The name to be displayed in WordPress Admin Panel.
  3. $capabilities: Privileges that one can access.

Complete list of all capabilities can be found here.

1. Administrator:

We didn’t delete the default administrator. That’s why, we’ll leave it with default privileges.

2. Moderator:

As we’ve already discussed, we’ll give him rights to create, edit, publish his own and other’s posts too.

Copy” below code and “Paste” it at the end of your “Theme Functions” file.

add_role('moderator', __(
    'Moderator'),
    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
        'edit_others_posts' => true, // Allows user to edit others posts too
        'publish_posts'     => true, // Allows the user to publish posts
        'manage_categories' => true, // Allows user to manage post categories
        )
);

Click on “Update File”.

Assign “Moderator” Role to a User:

From the WordPress Admin Panel.

Navigate to “Users > All Users” and follow instructions below.

We’ve assigned “Moderator” role to our user “Alex”. You can see from the image below that when he logs in and goes to “Dashboard”, he’ll be having limited access that we’ve allowed him.

3. Newbie:

As we’ve already discussed, we’ll give him rights to just edit his profile and create new posts.

Copy” below code and “Paste” it at the end of your “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, click on “Update File” like below image.

Assign “Newbie” Role to a User:

From the WordPress Admin Panel,

Navigate to “Users > All Users” and follow instructions below.

We’ve assigned “Newbie” role to our user “Dicaprio”. You can see from the image below that when he logs in and goes to “Dashboard”, he’ll be having limited access that we’ve allowed him.

Now, we’ll assign the “Newbie” role whenever a visitor registers.

 

Conclusion

By following the above tutorial, you can create custom user roles easily and manage multiple WordPress websites through a single window. Let me know if you have any issues in creating custom user roles by posting your comments below.

Additionally, keep an eye on this blog as I will be adding more resources for WordPress in the coming weeks. Likewise, you can start working by creating custom user roles using the method stated above or plugin that I’ve recommended and let me know what more features you want unlocked in it. 

Mustaasam Saleem :Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at mustaasam.saleem@cloudways.com