Reading list Switch to dark mode

    Create Custom Role In WordPress

    Updated 6 December 2018

    This blog describes about, how to Create Custom role in WordPress . A Role defines a set of tasks to which the user is allowed to perform. Here, tasks are the capabilities. A Capability tells user role what tasks/action that  role can perform like edit posts, delete posts, etc. So, Lets create custom role –

    The following snap shows the admin account having all the capabilities like add user, etc-

    Create Custom Role

     

    Creating Role

    Here is sample code of creating role in WordPress.

    Searching for an experienced
    WordPress Company ?
    Find out More

    Note: This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation.

    <?php
    register_activation_hook( __FILE__,  'create_role' );
    ?>

    The above code will execute the create_role function on activation of plugin
    (example).

    <?php 
    
    function create_role() {
    	add_role( 'test', 'Test Role', array(
    		'read' => true, // True allows that capability
    		'edit_posts' => true, // Allows user to edit their own posts
    		'publish_posts'=>true, //Allows the user to publish, otherwise posts stays in draft mode
    		'edit_published_posts'=>true,
    		'upload_files'=>true,
    		'delete_published_posts'=>true,
    	));
    }
    
    ?>
    

    The above code will create the ‘test’ role. Array in add_role function holds the capabilities given to this role. It shows with all the roles in add user page when admin is logged in.

     

    Now, Lets login with admin account. The following snap shows the new role in the list of roles.

    Create Custom Role

     

    Then add new user and assign custom role Test Role to it.

    To determine If the user role is set properly, log out of admin account and log in as new user. You will see the dashboard as shown below –

    Create Custom Role

    That’s all for how to create roles in WordPress.

    Support

    Still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

    Thanks for Your Time! Have a Good Day!

     

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    1 comments

  • Thakur Amit Chauhan
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content