Discover in this tutorial how to grant an extra role to a WordPress user using code, without plugins.
By default, WordPress has 5 user roles:
- Subscriber >
( 'subscriber' )
- Contributor > (
- Author > (
- Editor >
( 'editor' )
- Administrator >
( 'administrator' )
But a WordPress limitation is that it only allows you to grant one role to each user from its interface (a dropdown).
But what if you are interested in adding a new one?
There are plugins like Multiple Roles that allow you to do so, but you can achieve the same thing easily with just a few lines of code.
Let’s see how to do it:
Snippet to add an extra role to a WordPress user
Add the following code at the end of functions.php or in your functionality plugin:
With this snippet you first get the user with ID equal to 1681 and add the role you have already created earlier, in this case,
Remember that you can add a column that displays the WordPress user ID if you want to access that data easily.
By the way, the new capability is saved in the database so, once you’ve refreshed the WordPress admin page, which will make the code to run, you can delete the snippet.
Now you know how to assign an extra role to a WordPress user without plugins, with a simple snippet of code.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉