Kish
Kish

Reputation: 39

How to create custom users to manage group of users in WordPress?

I'm working on a woocommerce wordpress site with a admin user and many vendor users. I want to create another user type (manager of vendors) that can manage a group of vendor users. And only admin can assign vendors users to this new user type. So kind of like,

Admin -> Manager -> group of vendors.

And by managing, i mean add,edit,delete products of assigned vendor users and can also add new user of vendor role.

Edit- I've already defined a custom user as 'vendor_manager' type with 'list user', 'add user', 'delete user' capabilities. But it lists all users in 'vendor_manager' dashboard. What I want is to list only those users (of vendor type only) that admin assigns to 'vendor_manager'. For assigning, I created a menu in admin's dashboard with acf fields where I assign group of vendor users to this 'vendor_manager'. I just want to restrict 'vendor_manager' users to manage only assigned users. Hope it make sense.

Vendor Manager Role-

add_role( 
  'vendor_manager', 
  __( 'Vendor Manager', 'yourtextdomain' ), 
  array(
    'read' => true,
    'edit_posts' => true,
    'create_users'=> true,
    'delete_users'=> true,
    'edit_users'=> true,
    'list_users'=> true,
    'promote_users'=> true,
    'remove_users'=> true,
    'assign_product_terms'=> true,
    'delete_others_products'=> true,
    'delete_private_products'=> true,
    'delete_product'=> true,
    'delete_product_terms'=> true,
    'delete_products'=> true,
    'delete_published_products'=> true,
    'edit_others_products'=> true,
    'edit_private_products'=> true,
    'edit_product'=> true,
    'edit_product_terms'=> true,
    'edit_products'=> true,
    'edit_published_products'=> true,
    'manage_product_terms'=> true,
    'publish_products'=> true,
    'read_private_products'=> true,
    'read_product'=> true,
));

ACF options page to assign vendors to manager-

if( function_exists('acf_add_options_page') ) {

    acf_add_options_page(array(
        'page_title'    => 'Groups',
        'menu_title'    => 'Groups',
        'menu_slug'     => 'groups',
        'capability'    => 'edit_posts',
        'redirect'      => false
    ));
}

Assigning of vendors to managers-

Assigning of vendors to managers

Any help??

Upvotes: 1

Views: 389

Answers (1)

Saeed Jamali
Saeed Jamali

Reputation: 1195

You can create this custom role (vendor manager) by the below code and add privilege to that:

add_role( 
  'vendor_manager', 
  __( 'Vendor manager', 'yourtextdomain' ), 
  array(
    'read' => true,
    'edit_posts' => true,
    // Various Capabilities
));

also have a look at these 2 links for full citation:

https://www.cloudways.com/blog/add-custom-user-roles-in-wordpress/

https://www.ibenic.com/manage-wordpress-user-roles-capabilities-code/

Upvotes: 3

Related Questions