• There were some questions about changing needed capability to switch users. For example if your editors have capabilities to edit users, but you don’t want them to be able to switch.
    Here is solution for this. Put this into your fuctions.php and edit capability you need in user_switching_filter_user_has_cap function:

    global $user_switching;
    remove_filter( 'user_has_cap', array( $user_switching, 'filter_user_has_cap' ) );
    add_filter( 'user_has_cap', 'modify_user_switching_filter_user_has_cap' ), 10, 3 );
    
    function modify_user_switching_filter_user_has_cap( array $user_caps, array $required_caps, array $args ) {
    	$required_capability = 'manage_options';
    	if ( 'switch_to_user' == $args[0] ) {
    		$user_caps['switch_to_user'] = ( user_can( $args[1], $required_capability, $args[2] ) and ( $args[2] != $args[1] ) );
    	} else if ( 'switch_off' == $args[0] ) {
    		$user_caps['switch_off'] = user_can( $args[1], $required_capability );
    	}
    	return $user_caps;
    }

    Note that although this is quite clean way to change it, it might stop working, if author change the way, how the plugin handle capabilities…

    https://www.ads-software.com/plugins/user-switching/

  • The topic ‘Script to change needed capability to switch users’ is closed to new replies.