• 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

Adding a custom WordPress user role

Discover in this tutorial how to create a new custom WordPress user role without plugins, using code.

By default, WordPress has 5 user roles:

  • Subscriber
  • Contributor
  • Author
  • Publisher
  • Administrator

In addition to these, some Plugins can add new roles. For example, WooCommerce creates two more:

  • Customer
  • Store Manager

You’ve already learned how to remove the roles that don’t interest you or even get them back, but what if you want to generate a custom role?

WordPress has the following feature that makes the process easier:

Snippet to create a custom WordPress user role

To generate a new user role you can use the native function of add_role(), which has the following syntax:

add_role( $role, $display_name, $capabilities );

Where:

  • $role: Unique name of the role.
  • $display_name: Name displayed in the WordPress administration panel.
  • $capabilities: Privileges of the role.

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 fragment you create a new role with the name “Subscriber +” that has the ability to do the same as another subscriber plus create and edit their own posts.

This is just an example, but you can play with all the capabilities that WordPress allows. To learn about them all, take a look at their documentation on Roles and Capabilities.

Another interesting option would be to give that new user profile access to restricted web content. This can be done easily by combining the creation of a new profile with the Restrict Content Pro plugin.

Conclusion

Now you know how to generate a custom WordPress user role, without plugins, with a simple code snippet.

Any questions? I read you 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?