admin管理员组文章数量:1191726
I've been asked to help out a friend on a site he's cobbled together. I'm unfamiliar with Buddypress but on the members list page it's showing the site admin users, he'd like to exclude them but there are no plugin options to hide them.
I've had this Working to exclude one of them, but I now need to modify it to accept multiple user IDs.
Here's the current code.
add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
if ( 'members' != $object ){
return $query;
}
$excluded_user = '93'; // admin id to remove from the member directory
$args = wp_parse_args( $query );
if ( ! empty( $args['exclude'] ) ) {
$args['exclude'] = $args['exclude'] . ',' . $excluded_user;
} else {
$args['exclude'] = $excluded_user;
}
$query = build_query( $args );
return $query;
}
Any help would be much appreciated.
I've been asked to help out a friend on a site he's cobbled together. I'm unfamiliar with Buddypress but on the members list page it's showing the site admin users, he'd like to exclude them but there are no plugin options to hide them.
I've had this Working to exclude one of them, but I now need to modify it to accept multiple user IDs.
Here's the current code.
add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
if ( 'members' != $object ){
return $query;
}
$excluded_user = '93'; // admin id to remove from the member directory
$args = wp_parse_args( $query );
if ( ! empty( $args['exclude'] ) ) {
$args['exclude'] = $args['exclude'] . ',' . $excluded_user;
} else {
$args['exclude'] = $excluded_user;
}
$query = build_query( $args );
return $query;
}
Any help would be much appreciated.
Share Improve this question asked Jan 24 at 20:43 morgyfacemorgyface 3782 silver badges10 bronze badges2 Answers
Reset to default 2You could make an array of ids like $ids_to_exclude = array('93',...)
then loop through it using a forloop
or while
loop
$ids_to_exclude = array('93',...);
foreach ($ids_to_exclude as &$id) {
function_to_call_on_each_id($id);
}
The PHP manual has really good examples and is super easy to follow. PHP Foreach
Just thought I'd post my solution, I'm thinking it's verbose, but it works. I'll gladly receive any tips on how to reduce the bloat.
I included a little foreach loop which builds a list of IDs from usernames as I feel this would make more sense to anyone looking at this in future.
<?php
// Removes specified users
add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
if ( 'members' != $object ) {
return $query;
}
$usernames = array('Admin1', 'Admin2');
foreach ($usernames as $username) {
$user = get_user_by('login', $username);
$excluded_users[] = $user->ID;
} // Build array of user IDs from login usernames
$args = wp_parse_args( $query ); // Merges user query into array.
if ( ! empty( $args['exclude'] ) ) {
$args['exclude'] = $args['exclude'];
foreach ($excluded_users as &$id) {
$args['exclude'] .= ',' . $id; // Loop through building an array
}
} else {
$args['exclude'] = $excluded_users;
}
$query = build_query( $args );
return $query;
}
本文标签: phpWordPress Buddypress remove multiple users from members listStack Overflow
版权声明:本文标题:php - WordPress Buddypress remove multiple users from members list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738386115a2084191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论