Customer Happiness Manager at PressMate

September 21, 2017

How to add a user role in WordPress without using a plugin

WordPress offers number of user roles and allows different users to access different sections of websites. This is one of the key feature which is often overlooked by users. These user roles can help you to insure that only few people have access to just the areas where they want to. Restricting access to certain areas helps you in minimizing the chances of any misuse that can cause any serious damage to any WordPress website.

Since user roles have become an important part in WordPress from the release of WordPress version 2.0, many WordPress user don’t know that they can limit the user access of the dashboard. WordPress comes with six pre defined different user roles :

  1. Administrator – can access complete website
  2. Admin – can access all administration features
  3. Editor – create, edit, and publish their posts and others posts
  4. Contributor – create, edit and publish their posts
  5. Author – edit their own post, but cannot publish them
  6. Subscriber – manage their own profile

WordPress allows you to add new user roles using PHP giving you the freedom to arrange those roles accordingly. For instance,you may want a user to create / edit posts, manage categories, edit pages and more importantly it is equally necessary for them to avoid few things like edit themes, update core, and add or remove plugin.

WordPress doesn’t offer any kind of inbuilt graphic interface for creating roles. There is an easiest way to add roles in WordPress by using PHP  function that allows you to specify exactly what permission to assign to an user.

The function to add user with custom permissions is  “add_role”.  

This function takes three parameters as follows :

  1. The Internal role Name
  2. The Display Role
  3. An Array of Permission

A detailed explanation of this function can be found at WordPress codex 

Take a look at the following function that we often use to create a user type “Client” that has access to all the features but plugin management. Just copy paste the following code in your theme’s functions.php file.


This is by far the most simple way to add custom user roles if you know a bit of coding in WordPress. Once you’re done adding the above code, you will find a new user type in your Add user section ( check the screenshot below )

Conclusion

  1. WordPress doesn’t have an in built user management system.
  2. You can use a plugin to manage the user types.
  3. If you want to just create your own user type, then you can use add_role function.
  4. Once a user role is created, it stays in the WordPress database forever. You can then go ahead and remove the add_role code snippet that you add in your theme’s functions.php file.

We hope this article was useful for you and helped you with creating your own custom user type 🙂

0

Enjoyed the article?

Press 💚 below and join the other 13,000+ getting valuable updates from this blog.

Get Free Site Analysis

Want us to analyze your site first and then use our services? No worries. Just fill out the form to the right and we will be happy to send you a free report about your current website.