0

Is there any plugin or script where i can use to define maximum number of users with a specific role on subsite in a multisite network?

I'm offering cloud helpdesk wordpress and pricing is based on number of agents, so I want to make sure that site admin is not able to add more agents than the predefined.

Thanks a lot in advance

2

Ok here is how I did it:

add_action( 'editable_roles' , 'hide_editable_roles' );
function hide_editable_roles( $roles ){
$blog_id = get_current_blog_id(); // Get current subsite id
switch($blog_id) { // Define different max agents numbers depending on subsite id
    case 6: 
    $max_agents = 10; //for subsite id#6 we can have maximum 10 agents
     break;  
    case 7: //for subsite id#7 we can have maximum 3 agents
    $max_agents = 3;
    break;  
    default:
    $max_agents = 3000; //default is 3000 agents
    break;
}
  $agents = get_users( array( 'role' => 'agent' ) ); // here you define the role
  $agents = count( $agents );
    if ($max_agents <= $agents){
        unset( $roles['agent'] ); // here you define the role
    }
    return $roles;
}
| improve this answer | |
  • Quite indeed, good call. I totally forgot editable_roles filter's existance. – Antti Koskinen Jun 6 '19 at 11:01
0

Update: There was a problem with my previous code based on some erroneous digging into WordPress roles and capabilities. The below code should work a little better now.

You could maybe try hooking the map_meta_cap filter, something like this might work:

add_filter( 'map_meta_cap', 'has_max_agents', 10, 2 );

function has_max_agents( $caps, $cap ) {
    static $max_agents = 30; // Example max number of agents.
    if ( 'create_users' !== $cap ) {
        return $caps;
    }

    $users = get_users( [
        'role__in' => [ 'agent' ], // Your custom role in place of 'agent'
    ] );

    if ( true || $max_agents <= count( $users ) ) {
        foreach ( $caps as $index => $maybe_remove_cap ) {
            if ( 'create_users' !== $maybe_remove_cap ) {
                continue;
            }

            unset( $caps[ $index ] );
        }
    }

    $caps[] = 'do_not_allow';

    return $caps;
}
| improve this answer | |
  • unfortunately didn't work, thanks though. – Akram Jun 5 '19 at 23:15
0

Maybe using insert_user_meta (inside wp_insert_user(), which is also called by wp_update_user()) could also be a viable method to block extra agents from being registered. set_user_role action might also be needed to prevent existing users from getting an agent role.

// Maybe prevent setting new users to agent role
function new_user_check_roles( $meta, $user, $update ) {

  $max_agents = 30;
  $agents = get_users( array( 'role' => 'agent' ) );
  $agents = count( $agents );

  if ( $max_agents != $agents ) {
    return $meta;
  }

  if ( in_array( 'agent', $user->roles ) ) {
    $user->set_role( 'subscriber' );
  }

  if ( isset( $meta['role'] ) && 'agent' == $meta['role'] ) {
    $meta['role'] = 'subscriber';
  }  

  return $meta;
}
add_filter( 'insert_user_meta', 'new_user_check_roles', 10, 3 );

// Maybe prevent setting existing user to agent role
function set_roles_check_role( $user_id, $role, $old_roles ) {

  $max_agents = 30;
  $agents = get_users( array( 'role' => 'agent' ) );
  $agents = count( $agents );

  if ( $max_agents != $agents ) {
    return;
  }

  if ( 'agent' !== $role ) {
    return;
  }

  $user = new WP_User( user_id );
  $user->set_role( 'subscriber' );
}
add_action( 'set_user_role', 'set_roles_check_role', 10, 3 );
| improve this answer | |
  • unfortunately didn't work, thanks though. – Akram Jun 5 '19 at 22:43
  • forgot to mention it's a multisite network, not sure if this is the reason why it didn't work – Akram Jun 5 '19 at 23:12
  • @Akram Are the users created in the subsite (by administrator) or the network (by super admin) user view? – Antti Koskinen Jun 6 '19 at 5:49
  • They are created by the subsite administrator – Akram Jun 6 '19 at 7:44
  • @Akram Yes, lets discuss more on LinkedIn. – Antti Koskinen Jun 6 '19 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.