DEV Community

Cover image for How to create custom roles in Wordpress
Chetan Rohilla
Chetan Rohilla

Posted on • Edited on • Originally published at w3courses.org

How to create custom roles in Wordpress

A role defines a set of capabilities for a user. For example, what the user may see and do in his dashboard.

By default, WordPress have six roles:

  • Super Admin
  • Administrator
  • Editor
  • Author
  • Contributor
  • Subscriber

Sometimes we need to create custom roles in our website for users management. Here we can create custom roles in wordpress.

Create custom roles for wordpress with plugin

User Role Editor – User Role Editor WordPress plugin allows you to change user roles and capabilities easy.

Members – Membership & User Role Editor Plugin – Members is a roles and capabilities based WordPress membership plugin. It gives your users the ultimate member experience by giving you powerful tools to add roles and capabilities and assign them to your users.

Advanced Access Manager – Content access control on the frontend, backend and API levels to posts, pages, media attachments, custom post types, categories, tags, custom taxonomies for any role, user and visitors. Roles & capabilities management with the ability to create new roles and capabilities, edit, clone or delete existing.

WPFront User Role Editor – WPFront User Role Editor plugin allows you to easily manage WordPress user roles within your site. You can create, edit or delete user roles and manage role capabilities.

Create custom roles for wordpress without plugin

If you want to create custom roles without plugin then paste the code given below in your functions.php file under your themes directory.

add_action('after_setup_theme','client_add_role_function');

function client_add_role_function(){

    $roles_set = get_option('client_roles_are_set');

    if(!$roles_set){

        $result = add_role( 'client', __(

            'Client' ),

        array(

            'read' => true, 
            'edit_posts' => true, 
            'edit_pages' => true, 
            'edit_others_posts' => true, 
            'create_posts' => true, 
            'manage_categories' => true, 
            'publish_posts' => true, 
            'edit_themes' => false, 
            'install_plugins' => false, 
            'update_plugin' => false, 
            'update_core' => false 

        )

    );

        update_option('client_roles_are_set',true);

    }

}
Enter fullscreen mode Exit fullscreen mode

Now we have our custom role Client and we can create custom users with this roles. In the code above we have used the wordpress function add_role. We have also added some capabilities or permissions to our custom role Client –

  • read => true, Allow user to read the dashboard
  • edit_posts => true, Allow user to edit their own posts
  • edit_pages => true, Allow user to edit pages
  • edit_other_posts => true, Allows user to edit others posts not just their own
  • create_posts => true, Allows user to create new posts
  • manage_categories => true, Allows user to manage post categories
  • publish_posts => true, Allows the user to publish, otherwise posts stays in draft mode
  • edit_themes => true, User can’t edit your theme
  • install_plugins => false, User can not add new plugins
  • update_plugin => false, User can not update any plugins
  • update_core => false, User can not perform core updates

Finally we used the wordpress hook after_setup_theme. You can read here in detail.

If you want to create custom menu in frontend for this custom role then read our article how to create menu for loggedin users in wordpress.

You can also create a custom templates for custom user roles. Just follow the article how to create custom post template in wordpress and add condition of this user role.

Using the code given below you can get the current user role in wordpress.

if( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles; // This returns an array
}
Enter fullscreen mode Exit fullscreen mode

Please like share subscribe and give positive feedback to motivate me to write more for you.

For more tutorials visit my website.

Thanks:)
Happy Coding:)

Top comments (0)