Easy Custom User Role Feature Without a WordPress Plugin

How to add custom user roles to your theme without using a third party WordPress plugin.

Welcome back to another WordPress tutorial. This time I will show you how to add and create a new Custom User Role and assign its own capabilities. This feature may come up handy if the default WordPress user roles (Author, Subscriber, Editor, etc.) aren’t sufficient for your needs and you want to create a custom one with its own capabilities.

Custom User Roles

Before we move forward you can preview the final result of the script in the screenshots below.

The Alternative Options

There are quite a bit of plugin options out there so you might want to go ahead and browse the WordPress.org directory. However, if you are working on a custom solution or you don’t really want to use a plugin you can take a look at the script and use it as a starting point.

The Assets

I tried to keep the code as simple as I could but there are some additional customizations you might want to remove or replace with your own.

I am loading the Font Awesome 4.7 icons only for demonstration purposes and you may want to remove them if you want to use another method.

However, if you already have them loaded within your plugin or a theme you keep the code as it is.

Note: I suppose you can have the same end result by using the WordPress built-in Dashicons or another method e.g. SVGs, CSS Entities or even Emojis.

Subscribe to our Newsletter* if you find our content useful and would like to receive regular updates with the latest posts.

The Setup

For this demo tutorial, I am loading the Custom User Role feature inside my theme functions.php file and adding it as an additional setting under Settings > General.

Of course, you can reuse and tweak the code and apply the same approach to your custom plugin.

The Code

There are a few comments added inside the PHP file, but I want to make a couple more clarifications before I show you the actual code.

  • I am using multiple select and using FA to apply a CSS hack so the select has checkboxes. Additionally, there’s a jQuery script that allows us to select capabilities without the need to hold CTRL or CMD keys.
  • When you delete the Custom User Role all the users that have this role assigned are kept, we only switch their role back to the default WordPress Subscriber user role.
  • Lastly, you currently cannot edit the Custom User Role capabilities once you create them. But you can easily extend the script and add the functionality with a little bit of additional code.

What’s Next?

As I mentioned you may want to edit your custom user roles and to have this you need to tweak a bit the code. The same way I listed the user roles for removal I would create a select dropdown and then populate the existing capabilities for the selected role.

The two WordPress function that will come up handy to create the edit functionality are has_cap() and remove_cap(). Additional information can be found at the WordPress Codex.

Additionally, you may want to reorder the capabilities in the multiple select in a different way.

‘Til the next one.

* We have a NO SPAM policy so you won't reaceive any meaningless emails from us!

Related Content

How to Add a Simple Plain HTML Search with Autocomplete in WordPress

Integrate plain HTML search form and override WordPress get_search_form function.


Create Gutenberg Blocks with ACF 5.8+ PRO

How to register and customize Gutenberg blocks with the Advanced Custom Fields plugin (no JavaScript needed)


Integrate Bitly Short URLs Within WordPress

How to use Bitly to generate short URLs and track your social media clicks.


Leave a Reply

Your email address will not be published. Required fields are marked *

Adding License Key Feature to Your Premium WordPress Theme or Plugin (3/3)
Create a Complete WordPress Blogger Card Plugin Using Web Components

Thank you for reading our content. Use the buttons below to spread the love and share Integrate Bitly Short URLs Within WordPress with your followers.