How to Create Custom User Roles in WordPress

Bespoke User Roles- Creating them for 100% WordPress Website Uptime

Availability of different user roles is perhaps one of the most stunning features of WordPress which has helped the CMS in standing out from the crowd. Holding a specific amount of significance in ensuring the uptime of your website, user roles can be created easily. In this post, I’ll be looking at what user roles in WordPress mean and how you can proceed ahead with creating custom user roles in a convenient way.

WP custom user roles

Understanding User Roles in WordPress

WordPress comes equipped with six default user roles as explained below:

Get 20% off Logo and Ad Design by using coupon 85IDEAS

Get 20% off Logo and Ad Design by using coupon 85IDEAS

  • Administrator– this is the user who has access to all the administrative features and function within the site

  • Editor– this is the person who has the freedom of managing and publishing posts belonging to different user, including his/her own
  • Author– this is the person who can easily publish and manage his/her posts

  • Contributor– this is the person who is offered the freedom of writing and managing his/her posts but can’t publish the same at his/her will

  • Subscriber– this is someone who can just manage his/her profile

What’s the need for creating custom user roles in WordPress?

Well, a lot of website owners choose to create custom roles for their site just to ensure that their users/clients have access to what they need. Rather than being able to access everything available on the website. Creating custom user roles also serves as the right option for ensuring the 24/7 uptime status of your WordPress website.

A walk through basic WordPress functions

WordPress comes with five default functions as explained below:

  • add_role- it enables you to add a custom role

  • remove_role()- it enables you to remove a custom role

  • add_cap()- it enables you to add a custom capability to a particular role

  • remove)cap()- it enables you to remove a custom capability from a specific role

  • get_role()- it enables you to fetch information about a particular role in addition to its related capabilities

  • Getting started with creation of a custom user role using the add_role() function

    Step 1- Define the user role

    As the very first step, you need to assign a name to your custom user role. Since this tutorial teaches you creation of user role for a customer, I’ve chosen to name the user role as “Customer”. Additionally, here is the list of capabilities that would be implied by the person with a “Customer” user role:

    • Creating posts

    • Editing posts

    • Editing other posts

    • Managing post categories

    • Editing pages

    Apart from above, the capabilities that won’t be available to this individual include the following:

    • Editing themes

    • Adding or removing plugins

    • Updating core

    Step 2- Write the code for custom user role

    Here is the code snippet that you need to add to your current WordPress theme’s functions.php file:

    // define a custom user role

    $output = add_role( ‘customer’, __(

    ‘Customer’ ),

    array( ) );

    With that, you’re done with creating a new user role.

    Step 3- Adding functionality to the newly created custom user role

    Since the new custom user role doesn’t have any functionality assigned to it, you need to add the same. For this, you can simply add an array code to the snippet that has already been entered in the theme’s functions.php file. Here’s the array code that I’m talking about:

    // Add a custom user role

    $output = add_role( ‘customer’, __(

    ‘Customer’ ),

    array(

    ‘read’ => true, // true denotes activation of this capability

    ‘edit_posts’ => true, // Allows user to edit their own posts

    ‘edit_pages’ => true, // Allows user to edit pages

    ‘edit_others_posts’ => true, // Allows user to edit others’ posts and 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 stay in draft mode

    )

    );

    Moreover, the code snippet associated with restricting certain functionality for the customer is shown below:

    // Add a custom user role

    $output = add_role( ‘customer’, __(

    ‘Customer’ ),

    array(

    ‘read’ => true, // true denotes activation of this capability

    ‘edit_posts’ => true, // Allows user to edit their own posts

    ‘edit_pages’ => true, // Allows user to edit pages

    ‘edit_others_posts’ => true, // Allows user to edit others posts and 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’ => false, // false denotes deactivation of this capability. User can’t edit your theme

    ‘install_plugins’ => false, // User cant add new plugins

    ‘update_plugin’ => false, // User can’t update any plugins

    ‘update_core’ => false // user can’t perform core updates

    )

    );

    That’s it!

    Wrapping Up

    Custom user roles have proved beneficial for affirming authorized access to critical areas of your WordPress website. Here’s hoping the above post would’ve enlightened you regarding the method used for creating custom user roles in a simple and effective format.

    This article was contributed by Sophia Phillips no payment or incentive was given for this post.

    A bit about our guest: 

    Sophia Phillips has been working as a professional in custom WordPress development company called WordPrax and loves sharing information about leveraging multiple benefits of WordPress CMS in the best possible manner. Currently, she has an impressive count of WordPress web development-related articles under her name.

    Editorial Staff
    mail@85ideas.com

    Editorial Staff at 85ideas is a team of WordPress experts led by Brian Harris. Here to share amazing tuts, guides and collections.

    2 Comments
    • dandres077
      Posted at 19:12h, 17 June Reply

      Hi, excellent post, very nice. I created a menu with the following code:

      function theme_options_panel(){
      add_menu_page('Theme page title', 'Theme menu label', 'manage_options', 'theme-options', 'wps_theme_func');
      add_submenu_page( 'theme-options', 'Settings page title', 'Settings menu label', 'manage_options', 'theme-op-settings', 'wps_theme_func_settings');
      add_submenu_page( 'theme-options', 'FAQ page title', 'FAQ menu label', 'manage_options', 'theme-op-faq', 'wps_theme_func_faq');
      }

      add_action('admin_menu', 'theme_options_panel');
      function wps_theme_func(){
      echo '
      Theme';
      }

      function wps_theme_func_settings(){
      echo '
      Settings';
      }

      function wps_theme_func_faq(){
      echo '
      FAQ';
      }

      I created a new role to only access the menu I just created:

      $result = add_role( 'invitador', __('Invitador' ),array(
      'wps_theme_func' => true,
      'wps_theme_func_setting' => true,
      'wps_theme_func_faq' => true,
      ));

      I think inviting a user with the role.

      when I try to throw me a permissions error .

      I can help determine because it works for me.

      thank you very much .

      • Brian H
        Posted at 15:44h, 23 June Reply

        Hi @dandres077:disqus

        Sorry for the delay in my reply I am happy it worked out well for you. Thank you for stopping by.

    Post A Comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.