🥽 Beta testers needed! Stay up to date with the latest news from CTRLS.DEV

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

Overwrite and Send Custom Emails in WordPress

How to send custom emails using wp_mail() and overwrite all WordPress registration emails.

READ MORE...

Track your WordPress.org Plugin Stats on Your Site

How to track your WordPress.org plugins statistics and display them as a widget within your own theme.

READ MORE...

Adding License Key Feature to Your Premium WordPress Theme or Plugin (2/3)

How to set up our API server and link up the previously created functions with our server app.

READ MORE...

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 (1/3)
Track your WordPress.org Plugin Stats on Your Site

Thank you for reading our content. Use the buttons below to spread the love and share Adding License Key Feature to Your Premium WordPress Theme or Plugin (2/3) with your followers.