Exclude Users from Members directory on a BuddyPress based social network
Jotted by Brajesh Singh in Buddypress, Buddypress Tricks on January 29, 2012
Hi All,
I hope you all are busy building the next awesome social network.
It has been a long time since I wrote my last post. I was busy setting up an office here in Mohali,Punjab. Everything is setup now, and I am almost back to regular work.
Today, we will see how to exclude some members from the members directory of BuddyPress. I have seen this question numerous times and finally I thought to put a small tutorial. It is quick and easy tutorial, so Let us begin.
We will need to hook to 'bp_ajax_querystring' filter.
The following code will allow to exclude the users from the members directory. They will be still listed in the friends list of other users with whom they are friends with.
add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
//list of users to exclude
$excluded_user='1,2,3';//comma separated ids of users whom you want to exclude
if($object!='members')//hide for members only
return $qs;
$args=wp_parse_args($qs);
//check if we are listing friends?, do not exclude in this case
if(!empty($args['user_id']))
return $qs;
if(!empty($args['exclude']))
$args['exclude']=$args['exclude'].','.$excluded_user;
else
$args['exclude']=$excluded_user;
$qs=build_query($args);
return $qs;
}
You can put the above code in functions.php of your theme or in bp-custom.php.
Everything is fine and the users will be gone from the list. Now, comes a decision point. do you want them to be searchable ? The above code will hide them from the search too.
So, let us improve on our code .
add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
//list of users to exclude
$excluded_user='1,2,3';//comma separated ids of users whom you want to exclude
if($object!='members')//hide for members only
return $qs;
$args=wp_parse_args($qs);
//check if we are searching or we are listing friends?, do not exclude in this case
if(!empty($args['user_id'])||!empty($args['search_terms']))
return $qs;
if(!empty($args['exclude']))
$args['exclude']=$args['exclude'].','.$excluded_user;
else
$args['exclude']=$excluded_user;
$qs=build_query($args);
return $qs;
}
That’s it. All we are doing is checking if the query string is for listing members and we modify it to exclude the users.
I hope the code will help a couple of you.
Looking forward to hear your feedbacks