Categories

Sunday, November 24, 2024
#919814419350 therichposts@gmail.com
WordpressWordpress HooksWordpress Tricks

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

Wordpress trick to check term id has parent term or child term

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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