0

SOLVED! CHECK THE END TO VIEW SOLUTION

I have this code which hides users from the user search. there is a always hidden user, who can see everyone else and is always hidden his user role dosent matter and then there is 1stuser and 2nduse who im manually typing their usernames (below).. but a commonality between these 2 users is that they are in the same user role called "semihidden"

how do I dynamically extract the usernames from everyone in a role "semihidden" and them once that array is made, modify it through string modification to make it look like

array (''alwayshiddenuser'',''1stuserofrole'',''2nduserofrole'' )

which first step should something like adding the always hidden user to said array, and then doing some modification to make it look like above?

once we do that, I can just save that output as $comparednames and code should be working great!

Thanks everyone!

/* EDIT USERNAME HERE - HIDE SPECIFIC ADMINS FROM OTHERS - MAKE SURE TO DISABLE THEME CHANGE ON AAM FOR ALL OTHER ROLES... or else they can just change functions.php lol
*/
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  global $current_user;
  $username = $current_user->user_login;


//maybe pull all usernames from a role, and then modify array to have the \' and then input that into the function below
  if ($username != 'alwayshiddenuser')  { 
  $comparednames = array ('\'alwayshiddenuser\'','\'1stuserofrole\'','\'2nduserofrole\'' ); // \is an escape function for next character
  foreach ($comparednames as $comparedname) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
//you cannot see these users names
      "WHERE 1=1 AND {$wpdb->users}.user_login != " . $comparedname ,$user_search->query_where);
  } 
  
  }
}

well I found this code

//this sucessfully gets all user IDs of many roles
    global $wpdb;
    $roles = array('adminsub');
    if ( ! is_array( $roles ) )
        $roles = array_walk( explode( ",", $roles ), 'trim' );
    $sql = '
        SELECT  ID, display_name
        FROM        ' . $wpdb->users . ' INNER JOIN ' . $wpdb->usermeta . '
        ON          ' . $wpdb->users . '.ID             =       ' . $wpdb->usermeta . '.user_id
        WHERE       ' . $wpdb->usermeta . '.meta_key        =       \'' . $wpdb->prefix . 'capabilities\'
        AND     (
    ';
    $i = 1;
    foreach ( $roles as $role ) {
        $sql .= ' ' . $wpdb->usermeta . '.meta_value    LIKE    \'%"' . $role . '"%\' ';
        if ( $i < count( $roles ) ) $sql .= ' OR ';
        $i++;
    }
    $sql .= ' ) ';
    $sql .= ' ORDER BY display_name ';
    $userIDs = $wpdb->get_col( $sql );
    print_r($userIDs);

and this uses sql to pull all the user IDs from a role, and put them into an array, which is super helpful... but I need to not get user IDs but instead get usernames lol... and im not sure what to change?

SOLVED just swap TYPE1USERNAMEHERE for your username(s) which is hidden from everyone and will also beable to see everyone and swap TYPE1ROLETOHIDEHERE for your role(s) you want to hide from others except the above username(s)

/* EDIT USERNAMES AND ROLES - Hide users from others -  DISABLE THEME CHANGE ON AAM FOR ALL OTHER ROLES... or else they can just change functions.php lol
*/
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  global $current_user;
  $username = $current_user->user_login;

 //BOTH statements MUST be true to proceed, if one fails the if statement is cancelled
 $hiddenlist = array('\'TYPE1USERNAMEHERE\'' , '\'TYPE2USERNAMEHERE\'');
 $roles = array('TYPE1ROLETOHIDEHERE , TYPE2ROLETOHIDEHERE');
 //THESE USERS ARENT AFFECTED BY THIS FUNCTION AND CAN SEE EVERYTHING
  if (($username != 'TYPE1USERNAMEHERE') && ($username != 'TYPE2USERNAMEHERE'))
  { 
    //start of test
//this sucessfully gets all user IDs of many roles
    global $wpdb;
    if ( ! is_array( $roles ) )
        $roles = array_walk( explode( ",", $roles ), 'trim' );
    $sql = '
        SELECT  ID, display_name
        FROM        ' . $wpdb->users . ' INNER JOIN ' . $wpdb->usermeta . '
        ON          ' . $wpdb->users . '.ID             =       ' . $wpdb->usermeta . '.user_id
        WHERE       ' . $wpdb->usermeta . '.meta_key        =       \'' . $wpdb->prefix . 'capabilities\'
        AND     (
    ';
    $i = 1;
    foreach ( $roles as $role ) {
        $sql .= ' ' . $wpdb->usermeta . '.meta_value    LIKE    \'%"' . $role . '"%\' ';
        if ( $i < count( $roles ) ) $sql .= ' OR ';
        $i++;
    }
    $sql .= ' ) ';
    $sql .= ' ORDER BY display_name ';
    $userIDs = $wpdb->get_col( $sql );
    //now $userIDs is an array of all user ID of the matched role

    //hide these users with matching IDs
    foreach ($userIDs as $comparedID) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.ID<>" . $comparedID ,$user_search->query_where);
    } 
    //hide these users with matching usernames
    foreach ($hiddenlist as $comparedname) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
     "WHERE 1=1 AND {$wpdb->users}.user_login != " . $comparedname ,$user_search->query_where);
    }
   
  }
}
2
  • This has nothing to do with WordPress. It’s purely a PHP matter, and well documented: php.net/manual/en/language.types.string.php Commented Jan 30, 2022 at 11:17
  • Ah Jacob sorry, I clarified my request as its a little more advanced then I posted
    – danem
    Commented Jan 30, 2022 at 11:53

1 Answer 1

0

just swap TYPE1USERNAMEHERE for your username(s) which is hidden from everyone and will also beable to see everyone and swap TYPE1ROLETOHIDEHERE for your role(s) you want to hide from others except the above username(s)

/* EDIT USERNAMES AND ROLES - Hide users from others -  DISABLE THEME CHANGE ON AAM FOR ALL OTHER ROLES... or else they can just change functions.php lol
*/
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  global $current_user;
  $username = $current_user->user_login;

 //BOTH statements MUST be true to proceed, if one fails the if statement is cancelled
 $hiddenlist = array('\'TYPE1USERNAMEHERE\'' , '\'TYPE2USERNAMEHERE\'');
 $roles = array('TYPE1ROLETOHIDEHERE , TYPE2ROLETOHIDEHERE');
 //THESE USERS ARENT AFFECTED BY THIS FUNCTION AND CAN SEE EVERYTHING
  if (($username != 'TYPE1USERNAMEHERE') && ($username != 'TYPE2USERNAMEHERE'))
  { 
    //start of test
//this sucessfully gets all user IDs of many roles
    global $wpdb;
    if ( ! is_array( $roles ) )
        $roles = array_walk( explode( ",", $roles ), 'trim' );
    $sql = '
        SELECT  ID, display_name
        FROM        ' . $wpdb->users . ' INNER JOIN ' . $wpdb->usermeta . '
        ON          ' . $wpdb->users . '.ID             =       ' . $wpdb->usermeta . '.user_id
        WHERE       ' . $wpdb->usermeta . '.meta_key        =       \'' . $wpdb->prefix . 'capabilities\'
        AND     (
    ';
    $i = 1;
    foreach ( $roles as $role ) {
        $sql .= ' ' . $wpdb->usermeta . '.meta_value    LIKE    \'%"' . $role . '"%\' ';
        if ( $i < count( $roles ) ) $sql .= ' OR ';
        $i++;
    }
    $sql .= ' ) ';
    $sql .= ' ORDER BY display_name ';
    $userIDs = $wpdb->get_col( $sql );
    //now $userIDs is an array of all user ID of the matched role

    //hide these users with matching IDs
    foreach ($userIDs as $comparedID) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.ID<>" . $comparedID ,$user_search->query_where);
    } 
    //hide these users with matching usernames
    foreach ($hiddenlist as $comparedname) {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
     "WHERE 1=1 AND {$wpdb->users}.user_login != " . $comparedname ,$user_search->query_where);
    }
   
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.