WordpressWordpress HooksWordpress Tricks

How to create new user role in WordPress and add capability to that role?

Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to create new user role in WordPress and add capability to that role?

WordPress is the best part for me in developer websites.

We will create new user role like “administrator” with wordpress custom functionality. We will create “employee” user role and we will also add capability like edit post, add post etc.

Here is the code and you need to add this code in your theme’s functions.php file:

function add_theme_caps() {
  add_role(
    'employee',
    __( 'Therichpost Employee' ),
    array(
      'read'           => true,  // true allows this capability
      'edit_posts'     => false,
      'delete_posts'   => false, // Use false to explicitly deny
      'manage_options' => false
    ) );

  // gets the author role
  $role = get_role( 'employee' );

       // add capability to the author role
  $role->add_cap( 'view_dashboard' );
}

add_action( 'admin_init', 'add_theme_caps' );

 

This is it, if you have any query related to this post or you want any kind information related wordpress then please let me know.

Jassa jatt

Thank you

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL