Categories
Advanced Tutorials

Add WordPress Custom User Roles

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.

Codeable.io

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 don’t really want to use a plugin, you can look at the script and use it as a starting point.

The Assets

I tried to keep the code as simple as possible, 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 result by using the WordPress built-in Dashicons or another method, e.g., SVGs, CSS Entities, or even Emojis.

The Setup

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

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

The Code

A few comments are added inside the PHP file, but I want to make a few 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 have this. It would help if you tweaked them a bit in 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 functions 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.

Leave a Reply

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