如何从自定义用户角色中提取所有用户名,然后执行一些数组修改

时间:2022-01-30 作者:danem

已解决!检查端到视图解决方案

我有一段代码,它将用户隐藏在用户搜索中。有一个总是隐藏的用户,他可以看到其他所有人,并且总是隐藏他的用户角色dosent matter,然后有1个用户和2个用户手动键入他们的用户名(如下所示)。。但这两个用户之间的一个共同点是,他们处于相同的用户角色中,称为;半隐藏“;

如何从角色中的每个人动态提取用户名;半隐藏“;一旦创建了这个数组,就可以通过字符串修改来修改它,使其看起来像

数组(\'\'alwayshiddenuser\',\'1stuserofrole\',\'2nduserofrole\')

第一步应该是向所述数组中添加始终隐藏的用户,然后进行一些修改,使其看起来像上面那样?

一旦我们这样做了,我就可以将输出保存为$ComparedName,代码应该工作得很好!

谢谢大家!

/* 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);
  } 
  
  }
}
我找到了这个密码

//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);
这使用sql从一个角色中提取所有用户ID,并将它们放入一个数组中,这非常有用。。。但我需要的不是用户ID,而是用户名,哈哈。。。我不知道要改变什么?

SOLVEDjust将Type1UserName替换为您的用户名,该用户名对所有人都是隐藏的,也可以看到所有人,并将TYPE1ROLETOHIDEHERE替换为您想要隐藏的角色,但上述用户名除外

/* 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);
    }
   
  }
}

1 个回复
SO网友:danem

只需将Type1UserName替换为您的用户名,该用户名对所有人都是隐藏的,也可以看到所有人,并将TYPE1ROLETOHIDEHERE替换为您想要隐藏的角色,但上述用户名除外

/* 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);
    }
   
  }
}

相关推荐

在WordPress表单上添加指向php字段文本的链接

我买了一个wordpress模板,但开发人员说他们不能帮我做我需要的。该主题集成了一个酒店预订系统,该表单为“放置”用户电子邮件提供了一个字段,但是该选项被禁用,只会显示一条文本,上面写着“需要登录”,但当我单击那里时,什么也没有发生。我想email字段至少应该把我带到一个页面(可能是用户注册),或者至少这是我希望发生的事情。我一直在查看该表单的PHP代码,但我真的不知道该怎么做,因为我不处理PHP,我不知道如何解决这个问题。我曾多次尝试通过添加a href=来解决这个问题,但什么都没有发生。有没有人可以