Home / Admin / Sensitive User Switch
Duplicate Snippet

Embed Snippet on Your Site

Sensitive User Switch

On a site with multiple admins, only allow user switching capacity to owner based on user name.

Code Preview
php
<?php
// Sensitive User Switch / Kiri Vadivelu / 2026
add_filter( 'user_has_cap', function( $allcaps, $caps, $args, $user ) {
    if ( 'switch_to_user' === $args[0] ) {
        // Deny switching to everyone except site owner
        if ( $user->user_login !== 'ownerusername' ) {
            $allcaps['switch_users'] = false;
        }
    }
    return $allcaps;
}, 9, 4 );  

Comments

Add a Comment