已解决!检查端到视图解决方案
我有一段代码,它将用户隐藏在用户搜索中。有一个总是隐藏的用户,他可以看到其他所有人,并且总是隐藏他的用户角色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);
}
}
}