More
    HomeTechnologyCreating Custom User Roles in WordPress

    Creating Custom User Roles in WordPress

    WordPress comes loaded with several useful features, however, a lot of them are often overlooked by users. One of the most overlooked feature is the different user roles included in WordPress – that enables you to assign access to users to only those areas that they need. However, this is a useful feature which helps lessen the count of accidents that tend to bring your website down.

    Through this post, I would like to provide a brief introduction on user roles, and will explain the process of how you can create custom roles based on your needs. Also, you’ll come to know why you need to create custom roles.

    Creating-Custom-User-Roles-in-WordPress-The-Why-and-How-Of-It

    - Advertisement -

    An Overview of User Roles

    User roles in WordPress, isn’t a new concept and has been available since the inception of WordPress version 2.0. But, unfortunately there are still many WordPress users out there, who are not aware of the availability of the feature – that allows them to assign different roles to multiple users. WordPress, by default, comes with 6 user roles such as:

    • Administrator: A user who has admittance to all the admin features as well as functions of a website;
    • Editor: Editor is the user who has capability of publishing and managing posts of all clients, including their own;
    • uthor: Users that can publish and manage only their own posts;
    • Contributor: one having the ability to write. Such a user can only manage his own posts, but cannot publish them;
    • Subscriber: one having the capability of managing their own profile.

    Need For Using Custom User Roles

    In most of the cases, default WordPress user roles are enough to meet your needs. But, you may come across situations when a user role you need to define won’t match with the default ones. This is where creating your own custom roles comes in handy.

    Creating custom roles, in fact, will provide you better control over assigning access to only those areas that your users need. If you’re creating a website for your clients, then most likely they’ll ask you to provide them with admin access. But, remember that it may only work if you’ve signed a maintenance contract with your client. However, in the case you’re responsible for managing your entire site and keeping it up 24/7, it would be better that your clients have restricted access to the website. And this can be achieved with the help of a custom user role.

    - Advertisement -

    Creating a custom role for your user will help you provide your clients with access to only the required yet important areas – that they’re needed to make their website more effective.

    Understanding the Process of Creating Custom Roles

    For managing user roles and capabilities in a WordPress site effectively, you’ll mostly have to make use of 5 different functions such as:

    • add_role()
    • remove_role()
    • add_cap()
    • remove_cap()
    • get_role ()

    In our case, we’ll only be using the add_role() function. This is because we’ll have to create a custom role for clients we don’t know about. Next, we need to define a name to user role. Let’s assume, that we want to keep the name to something like “Main User”. Once you’ve completed this process, you’ll probably be thinking about the role of your user (i.e. Main User). A WordPress install comes with 50 capabilities, however, it is recommended that the client should be assigned the following capabilities:

    • Create posts;
    • Edit posts;
    • Manage categories and so on.
    READ ALSO:  What is a Blu-Ray Player and Do You Need One?

    But, make sure that the client is not assigned roles such as:

    • Make edits to the theme;
    • Add any plugin(s) or remove it;
    • Update core functionalities of the theme.

    Next, you’ll have to write some code for creating custom roles that will be added to the functions.php file.

    For example, below is a code snippet that will help you attain your objective.

    // Code for adding a custom user role
    $result = add_role( ‘main_user’, __(
    ‘Main User’ ),
    array( ) );

    The above code will help you create a new custom role for your client. But, keep in mind that no functionality has been assigned to the client. And so, our next step will be to assign some capabilities (or functionalities) for the user.

    // Adding functionalities to a custom user role
    $result = add_role( ‘main_user’, __(
    ‘Main User’ ),
    array(
    ‘read’ => true, // true means that this capability is allowed
    ‘edit_posts’ => true, // user has the capability to edit their own posts
    ‘create_posts’ => true, // users can create new posts
    ‘manage_categories’ => true, // users have the capability to manage the posts
    ‘publish_posts’ => true, // users can publish the posts
    )
    );

    This code will add the functionality you would like to provide to your clients. But, to make sure that your client does not accidentally make your site crippled, simply add the following code in your theme functions file:

    // Adding functionalities to a custom user role
    $result = add_role( ‘main_user’, __(
    ‘Main User’ ),
    array(
    ‘read’ => true, // true means that this capability is allowed
    ‘edit_posts’ => true, // user has the capability to edit their own posts
    ‘create_posts’ => true, // users can create new posts
    ‘manage_categories’ => true, // users have the capability to manage the posts
    ‘publish_posts’ => true, // users can publish the posts
    ‘edit_themes’ => false, // this will unable users from editing the theme
    ‘install_plugins’ => false, // will make users incapable of installing plugins
    ‘update_plugin’ => false, // User won’t be able to update any plugin
    ‘update_core’ => false // user will be unable to perform core updates
    )
    );

    Final Words

    Hope that after going through this post, you’ll understand the need behind creating custom user roles, and will understand the process of creating them. However, once you complete creating custom roles, it is important to evaluate that your new user role works as intended. For this purpose, you just need to setup another user with some role. And then, log out of your site and log back in, signing up as the recently created user.

    Now based on the capabilities you’ve assigned and ones you’ve denied to the user, you should be able to see the desired user role in the dashboard.

    This article is written by Sophia Phillips. Sophia Phillips has been working as a professional in a custom WordPress development services company and loves sharing information about leveraging multiple benefits of WordPress in the best possible manner.

    - Advertisement -
    WPTidBits
    WPTidBitshttps://wptidbits.com/
    Owner of WPTidBits. Totally in love with WordPress!

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    110FansLike
    761FollowersFollow