Hide Administrators From Other Users
Removes administrator accounts from the Users screen for anyone who is not an administrator, for example shop managers on a WooCommerce site.
Opens WPIDE on
Code
add_action( 'pre_get_users', function ( $query ) {
if ( current_user_can( 'manage_options' ) ) {
return;
}
$excluded = (array) $query->get( 'role__not_in' );
$excluded[] = 'administrator';
$query->set( 'role__not_in', array_unique( $excluded ) );
} );
Notes
The role counts above the list are not changed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.