The same logic applies in this case, you just need to deny the ability for all users except your own (because administrators can switch by default).
Thanks John, I thought about that but this is actually not the real solution here, I think. The other admin (client in this case) can easily just create a new admin user. Suddenly, this admin user is able to switch to my “main” admin user
As I said, you’ll need to deny the ability from all users except your own.
I understood that I can deny the ability from all users. But how would you do that? From how I understand it, I have to do this for each individual user. If there is a new user, I need to add the new user again manually to the denial list.
e.g.
user “tom” is admin should not have user switching capabilities
user “susi” is admin should not have user switching capabilities
user “markus” is admin should have user switching capabilities
any new admin user from now on should not have user switching capabilities
That’s the provided code from the FAQ. How do I need to modify it?
add_filter( ‘user_has_cap’, function( $allcaps, $caps, $args, $user ) {
if ( ‘switch_to_user’ === $args[0] ) {
if ( my_condition( $user ) ) {
$allcaps[‘switch_users’] = false;
}
}
return $allcaps;
}, 9, 4 );
I think my question is valid and I hope there is a solution for this use case.
Thanks again for your support
Markus
It’s going to be something like the following ternary logic, but I can’t help you further because helping everyone with their customisations would be a full time job for me! 😄
add_filter( 'user_has_cap', function( $allcaps, $caps, $args, $user ) {
if ( 'switch_to_user' === $args[0] ) {
$allcaps['switch_users'] = ( $user->user_login === 'markussss' );
}
return $allcaps;
}, 9, 4 );
Best of luck
Of course .. thanks a lot!
best regards
Markus