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
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
Share Improve this question edited Jun 5, 2019 at 23:13 Akram asked Jun 3, 2019 at 19:22 AkramAkram 716 bronze badges3 Answers
Reset to default 2Ok 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;
}
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;
}
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 );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745432625a4627461.html
评论列表(0条)