Filter Super Admins for a WordPress Multi-Site

When building a WordPress Multi-Site SAAS product, you don't want users to view super admin accounts.

  • It can be confusing to see unknown emails in the admin.
  • Exposing the email can lead to unwanted contact.
  • Keep the users with all the power secret, it's better for security!
/**
 * Remove all super admins from a non-main site user list.
 **/
filter_super_admins( $args ) {
  global $wpdb;
  if ( ! is_main_site() ) {
    $user_logins = "'" . implode( "', '", get_super_admins() ) . "'";
    $prefix      = $wpdb->base_prefix;
    $query       = "SELECT ID from `" . $prefix . "users` WHERE user_login IN ( $user_logins )";
    $results     = $wpdb->get_results( $query );
    if ( ! empty( $results ) ) {
      $user_ids = array_map( function ( $user ) {
        return $user->ID;
      }, $results );
      $args['exclude'] = $user_ids;
    }
  }
  return $args;
}
add_filter( 'users_list_table_query_args', 'filter_super_admins' );

The function first finds all super users in the database, then filters out the super admin users by using the exclude argument in the user table query.

Thanks for reading. Code On!

Subscribe to our mailing list to receive updates about emerging tech trends, important security patches and really bad dad jokes.