Discover in this tutorial how to remove capabilities from a WordPress user role, without plugins, using code.
By default, WordPress has 5 user roles:
- Subscriber >
( 'subscriber' )
- Contributor > (
- Author >
( 'author' )
- Editor >
( 'editor' )
- Administrator >
( 'administrator' )
In addition to these, some plugins can add new roles. For example, WooCommerce creates two more:
- Store manager
Each one has some associated capabilities but it is possible that in some occasions you may be interested in simply restricting some of the capabilities of a role.
Let’s see how to do it:
Snippet to remove a capability to a WordPress user role
To remove permissions to a user role you can use the native
remove_cap() function. Let’s see an example where you remove the option to access the WordPress media library.
Add the following code at the end of functions.php or in your functionality plugin:
With this snippet you first call the role with the name “Author”
( 'author' ) and then remove its ability to view and add files to the WordPress library.
This is just an example, but you can play with all the capabilities that WordPress allows you. To know them all take a look at its documentation on roles and capabilities.
By the way, when you delete a capability it is saved in the database so once you have refreshed the page, which will execute the code, you can delete the previous snippet.
Now you know how to remove a capability to a custom WordPress user role, without plugins, with a simple code snippet.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂