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

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

by therichpost
0 comment
wordpress

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

You may also like

Leave a Comment

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