Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Create Custom Role In WordPress

    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-

     

    Creating Role

    Here is sample code of creating role in WordPress.

    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.

     

    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 –

    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!

     

    . . .

    Comment (1)

    Add Your Comment

  • Thakur Amit Chauhan
    Very helpful code indeed..!! thanks for sharing it
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index