How To Add Custom User Roles In WordPress: A Comprehensive Guide.

WordPress is a powerful platform with a wide range of features and capabilities. However, one of its greatest strengths – the ability to extend its functionality with plugins – can also be a bit of a weakness when it comes to security. With so many plugins available (both official and unofficial), it can be difficult to determine which ones are safe to install. In this blog post, we will show you how to add custom user roles to WordPress using both a plugin and code.
user roles
101 / 100

WordPress is a great platform, but it’s limited in what you can do without custom coding. If you want to add custom user roles to WordPress, you’ll need to do one of two things: use a plugin or add the code yourself. In this article, we’ll show you how to add custom user roles to WordPress using both methods.

Adding Custom User Roles with a Plugin If you want to add custom user roles to WordPress without any coding, your best bet is to use a plugin. In this article, we will show you how to add custom user roles to your WordPress site via a plugin and code.

WordPress Default User Roles

WordPress comes with a number of built-in user roles, which are designed to give you a good foundation for setting up your website. However, you will likely want to change some of the default settings to better suit your needs. WordPress comes with a few default user roles. These are Administrator, Editor, Author, Contributor, and Subscriber. Each role has a specific set of permissions that allow the user to do different things.

The administrator role is the most powerful role in WordPress, and it allows the user to do everything on the website. This is the role you will want to give to the person who is in charge of running the website.

The editor role allows the user to publish and manage posts and pages. The author role allows the user to publish and manage their own posts. The contributor role can write a blog post and make edits but they can’t publish it and finally, The subscriber role is applicable to sites with memberships or user profiles. At this level, a user can access and manage their site profile but that’s all. 

How to Use Permission Levels Correctly

When it comes to permissions levels on WordPress user roles, you have to be very careful about what you give people access to. If you give someone too much access, they could unintentionally or even intentionally damage your website. On the other hand, if you give someone too little access, they won’t be able to do their job properly.

For example, What is the point of assigning Administrator level permissions to someone contributing to your website on occasions?. Someone writing a one-time blog post doesn’t need to have access to all of your site’s back-end files. There’s also certainly no reason why they should be able to edit your site’s theme, add or remove plugins, or otherwise make site-wide changes. 

Why Custom User Roles?  

WordPress comes with a set of default user roles that you can use right out of the box. However, as your site grows, you may find that the default roles don’t quite fit your needs. In this case, you can create custom user roles to better suit your needs.

There are many different reasons to use custom user roles in WordPress. One of the most common reasons is to give users specific permissions for managing content. For example, you may want to give a user the ability to publish and edit articles, but not have access to the administrative dashboard.

Another common use for custom user roles is to create a more user-friendly experience for your visitors. For example, you may want to create a custom role for customers who are only interested in browsing your website. This role would give them limited access to the website, without the need to sign in or register.

Plugins you can use to add Custom User Roles in WordPress

Adding Custom User Roles with a Plugin. If you want to add custom user roles to WordPress without any coding, your best bet is to use a plugin. There are a few different plugins that allow you to do this:

1. User Role Editor

User Role Editor is a great plugin for WordPress that allows you to manage user roles and capabilities easily. It’s perfect for when you need to give specific users or groups of users specific permissions on your website. You can also use it to disable or enable features for different user roles.

User Role Editor is easy to use and install. After you’ve installed and activated it, you can go to the Users menu in WordPress and click on User Role Editor. This will take you to the main screen, where you can see all of the user roles and their capabilities.

User Role Editor is a WordPress plugin that allows you to manage user roles and capabilities easily. You can add, edit, delete, and suspend user roles. This is a very handy plugin for managing permissions on your WordPress site.

The User Role Editor plugin is available as a free and premium version. The free version is available from the WordPress plugin repository, and the premium version is available from the author’s website.

2. Capabilities

The Capabilities plugin for WordPress is a powerful tool that gives you more control over what users can do on your website. It’s perfect for creating a custom user experience for your visitors, or for restricting access to certain areas of your site.

The Capabilities plugin is easy to use. After installation, it adds a new menu to your WordPress dashboard. From there, you can create custom user roles and capabilities, and assign them to specific users or groups. You can also specify which features and pages are available for each role.

The Capabilities plugin is a great way to get a quick understanding of what a user can and cannot do on your website. It’s a simple, lightweight plugin that you can install in just a few minutes. Once it’s installed, you can see all of the capabilities that are assigned to each role on your website. This is a great way to troubleshoot permissions issues and make sure that users have the access they need to do their jobs.

3. Members

Adding a members plugin to your website is a great way to increase engagement and keep visitors coming back. There are a variety of different plugins to choose from, so it’s important to find one that fits the needs of your website.

Some plugins allow you to create a community around your website, while others give you the ability to sell products or services directly from your website. Whatever the purpose of your website, there is a members plugin that can help you achieve your goals.

WordPress has a default members plugin that you can use, or you can find a premium plugin. The default members plugin allows you to create custom user roles, and each role can have its own set of permissions.

For example, you might want to create a role for “contributors” who can write and publish articles, but not edit them. You might also want to create a role for “editors” who can write and publish articles, as well as edit them.

Adding Custom User Roles tWordPress with the User Role Editor Plugin

Getting started with the User Role Editor plugin is straightforward. Once installed, you should be able to go to Users > User Role Editor in the WordPress dashboard. From there, you can add a new user role by clicking Add Role on the right hand side of the screen.  

user role editor add new role

This will reveal a prompt where you can input a name for the new role and select a default user role to serve as the basis for it. Click Add Role again and then you can click the checkboxes next to Capabilities to add or remove which are assigned to your new user role.

user roles

Using Code to Add Custom User Roles in WordPress

Create a Custom Plugin

Theoretically, you can simply throw the code examples below into your theme’s functions.php file to run them. However, since changes to WordPress user roles are usually one-time events, it doesn’t make sense to do so. Inside the functions file, they will be loaded at every page refresh and constantly attempt to update your site’s database — not very efficient.

For that reason, it’s better to create a custom plugin in which you can include the code in question so that it will only run once when the plugin is activated. The first step for that is to, create a file named customize-user-roles.php (or whatever else you want to name it), open it with a code editor, and include the following snippet in it:

<?php

/*
	Plugin Name:  Add/Customize User Role
	Version:  1.0
	Description:  Plugin for adding/customizing WordPress user roles.
	Author:  Nick Schäferhoff
	Author URI:  https://www.nickschaeferhoff.com/
	License: GPLv2 or later
	Text Domain:  custom-user-roles
*/

This is simply a header that allows WordPress to understand that this is a plugin file. After saving, upload the it to wp-content/plugins on your website’s server (you can opt to give it its own directory or not). When done, you should be able to see it in the back end of your site.

custom plugin to create wordpress user roles in back end

From here, you can simply open the file again via an FTP client, input whatever code you need, and then execute it by simply activating and deactivating the plugin once.

Adding and Removing WordPress User Roles

Alright, first let’s talk about the add_role() function. According to the official WordPress Code Reference, this function will add a new user role to WordPress so long as one with the same name doesn’t already exist. Here is its basic structure:

add_role($role, $display_name, $capabilities = array() )

Let’s break it down what all these different parts mean:

  • $role — This is the name of the role as it will be saved in the WordPress database.
  • $display_name — The name of the role that users will see in the back end.
  • $capabilities — This array describes the capabilities of the new user role. You find a list of them here.

An Example

Let’s go over a concrete case to make it clearer. Let’s say you wanted to add a new role to your website called Maintenance Guy. The only thing that person is supposed to be able to do is update WordPress and any themes and plugins, nothing else. Here’s what the code snippet for that looks like:

function add_custom_user_role() {
	add_role( 'maintenance_guy', 'Maintenance Guy',
		array(
		'read' => true,
		'update_core' => true,
		'update_plugins' => true,
		'update_themes' => true
		)
	);
}

register_activation_hook( __FILE__, 'add_custom_user_role' );

As you can see, the role’s name is maintenance_guy and its display name is defined as well. The important part are the capabilities in the array. Besides read (which you need to add so that the person can even see the back end), it’s only update_coreupdate_plugins, and update_themes. The register_activation_hook at the function call is what makes it execute when activating the plugin.

If you activate the custom plugin, create a new user on your site with this role, and then log in with that user, all you should be able to see is the WordPress dashboard, the profile menu, and the update page.

wordpress custom user role

Now, whether or not it makes sense to create a user like that or simply automate the update process is a different story but the above should make it clear how the add_role function works. Now you can apply it to your own needs.

Removing WordPress User Roles

By the way, if you wanted to remove the role again and also use code, you would do so with the remove_role function. Just add this to your custom plugin and activate it once:

function remove_custom_user_role() {
    remove_role( 'maintenance_guy' );
}
 
register_activation_hook( __FILE__, 'remove_custom_user_role' );

Presto, the Maintenance Guy role is no more.

Adding and Subtracting Role Capabilities

In the above example, you can already clearly see how you can turn on and off capabilities with markup. However, you don’t need to always create new roles to change what users on your site can do. You can also add and subtract capabilities to existing or WordPress user roles.

For that, let’s first go over the add_cap() function. Here’s how you can use it to add capabilities to existing user roles:

function add_custom_user_caps() {
	$role = get_role( 'maintenance_guy' );
	$role->add_cap( 'install_plugins' );
	$role->add_cap( 'activate_plugins' );
	$role->add_cap( 'delete_plugins' );
	$role->add_cap( 'install_themes' );
	$role->add_cap( 'upload_themes' );
	$role->add_cap( 'switch_themes' );
	$role->add_cap( 'delete_themes' );
	
}

register_activation_hook( __FILE__ , 'add_custom_user_caps');

Building on what we know from before, this is relatively easy to understand. The function simply uses get_role to choose the user role you want to assign new capabilities to. This is followed by a list of new tasks you want users of this level to be able to perform. With the new capabilities, the Maintenance Guy user role now also has access to the plugin and theme menus in the WordPress back end.

custom wordpress user role with additional capabilities

Of course, you can also do the opposite and take away capabilities from user roles. For that, you can use the remove_cap() function. It works like so:

function remove_custom_user_caps() {
	$role = get_role( 'maintenance_guy' );
	$role->remove_cap( 'install_themes' );
	$role->remove_cap( 'upload_themes' );
	$role->remove_cap( 'switch_themes' );
	
}

register_activation_hook( __FILE__ , 'remove_custom_user_caps');

As you can see, it is basically the same as add_cap before. When executed, the above takes away the ability to manage themes from the Maintenance Guy role.

Assign Users to a Role 

Once you’re done tinkering with adding custom user roles and custom capabilities, it’s time to actually assign users to the roles you’ve designated. You can find all of this under Users > All Users.

Check the box next to any user you want to assign a role to or change their current role. At the top of the screen, use the drop-down menu that says Change role to… to select the user role you’d like to assign.

assign new role to wordpress user

Once selected, click the Change button. That’s literally all there is to it. 

Conclusion

Adding custom user roles to WordPress can be a very helpful way to manage access to your website. In this article, we showed you how to do it via plugin and also via code. If you have any questions, or if you need help adding custom user roles to WordPress, please let us know in the comments below. We’d be happy to help!

Read: How to quickly and easily fix 404 errors from your WordPress dashboard

How How to remove unused CSS and javascript from WordPress: A comprehensive guide

6 Methods to improve WordPress site search results instantly

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

side hustle

Online Side Hustles In Kenya That Pay Up To $50 Per Hour.

Kenyans are known for their entrepreneurial spirit, so it’s no surprise that there are many online side hustles you can consider. There’s no time like the present to start making some extra money. Whether you need to pay down debt, save for a rainy day, or just want to have a little more spending money, there are plenty of side hustles that can help you reach your financial goals.

affiliate programs

Should Freelancers Join Affiliate Programs?

As a freelancer, you may be wondering if you should join affiliate programs. The answer is: it depends. Affiliate programs can offer great benefits, but they may or may not be the best option for you.