• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Assigning an extra role to a WordPress user

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 > ( 'contributor' )
  • Author > ( 'author' )
  • Editor > ( 'editor' )
  • Administrator > ( 'administrator' )

In a previous tutorial you saw how to create a new user role with the capabilities you want.

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:

To see this and another 883 code snippets of this website, login or subscribe here.

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, course_editor.

User to which you have added an extra custom role
User to which you have added an extra custom role

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.

Conclusion

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. 😉

Admin area PHP WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?