Exclude Users from Members directory on a BuddyPress based social network
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Exclude Members from BuddyPress Members List. * * @param string $qs query string. * @param string $object object name(members, groups etc). * * @return string */ function bpdev_exclude_users( $qs = '', $object = '' ) { // 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; } add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 ); |
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, Here comes a decision point. Do you want them to be searchable ? The above code will hide them from the search too.
Let us improve on our code to keep the user searchable .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Exclude Users. * * @param string $qs query string. * @param string $object object(members,groups etc). * * @return string */ function bpdev_exclude_users( $qs = '', $object = '' ) { // 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; } add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 ); |
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 feedback ๐
Working. It's another good addition for our xtrabox services. Thanks Brajesh.
Would it be possible to include code to hide Super-Admin
(the first user/admin while installing WP/BP) in our private networks?!?
Til now I'm using PhpMyAdmin to set user_status from 0 to 99 to hide it.
Hi Hans,
Thank you for the comment.
I am glad it helped. putting "1" in the list of excluded users will hide superadmin from the list. Do you want to hide his complete profile btw?
Yes, complete.
What's now the difference between setting user_status from 0 to 99 (what I'm doing now) and setting ยซ1ยป with your code?!?
Hi Hans,
The difference is you are marking the user as inactive which in this case the user is active but not listed in the directory.
Hi,
Thanks for the post.
I was wondering if you could help me with taking this a stage further. Rather that individual users, would it be possible to hide an entire group of people based on their role? FOr example show only Authors and exclude subscribers and not make them searchable?
Regards
David
Hi David,
Thank you for the comment.
Here is a way to exclude the subscribers from search/directory
https://gist.github.com/2142009
Hope that helps.
Regards
Brajesh
Thanks for the reply on your other post. This is exactly what I was looking for. Not sure why it isn't working for me though. I used the first set of code because I don't want certain members found in search or listed in the member directory. Basically I only want subscribers found. I appreciate any help!
//Exclude users from Directory and searches.
add_action('bp_ajax_querystring','bpdev_exclude_users',1,764,769,770,771);
function bpdev_exclude_users($qs=false,$object=false){
//list of users to exclude
$excluded_user='1,764,769,770,771';//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;
}
I too was looking for exactly this. Thank you! Unfortunately, I'm having trouble integrating this with s2Member, specifically this line in the "exclude subscribers" version
$subscribers= get_users( array( 'role' => 'subscriber' ) );
I wish to exclude Level 0, or the "Free Subscriber" group. But replacing 'subscriber' with 'Free Subscriber' doesn't work, and I don't know what to put instead. Any ideas?
Well, I managed to get the line working with the following:
$subscribers= get_users( array( 'role' => __( 'subscriber' ) ) );
However, I noticed that while "Viewing member 1 to 20 (of 42 members)" has the correct number, the tab at the top (All Members) does not. It still says 48, including the 6 free subscribers. How might I fix this? Thanks!
Hi Robert,
for that purpose you will need to filter on this hook 'bp_get_total_member_count'
Hope that helps.
Regards
Brajesh
Hi Brajesh,
How would I modify the code to show different users in different tabs (or even pages) under buddypress members? As I understand the code now it always removes certain users. Would I add the action in the members-loop.php instead and then remove it again? And create lets say:
members-students.php, members-teachers.php, members-assistants.php to show different user roles?
I hope you know what I am trying to achieve….
Hi Sascha,
sorry for the delayed reply. I am unable to understand the goal. are you trying to achive tabls like all members, students, assistants or you are trying to show specific tabs when a user is logged in. In both the cases you can edit members/index.php and use conditionals to achieve it.
Hi Brajesh,
Thanks for your reply, late or not!
Yes, I am trying exactly that. I achieved some of it. Look at: http://www.landwire.de/members
But everytime I use the filters (newest, alphabetical, last active) the AJAX request destroys all my tabs. I think the search also returns all users and not only users of a certain role. I tried looking at the AJAX, but got completely lost. Not sure where it queries the DB and how I could filter that….
If you have a better idea, I would be glad to hear it.
Hi Sascha,
Thank you for the comment. Please allow me till this weekend and I will put a sample child theme of bp-default to show fetching users by role. That will be easier for you to understand and adapt.
Regards
Brajesh
Could not reply to your post below. But that would be marvellous to see what you will come up with. I used WP UI to create the tabs, so I am not sure if that would interfere with anything. Anyway, it does not necessarily have to be in tabs. Own pages are fine too, which brought me to think to create the different templates, which I did not get round to yet…
Hi Sascha,
Here is a child theme I created for you.
Please have a look at the functions.php and the members/index.php
https://github.com/sbrajesh/bp-list-users-in-dir-with-role
Hope that helps.
regards
Brajesh
Hi Brajesh,
Thank you for your post. I was looking for a way to exclude blogs created by super-admin to appear on the "Blogs" list page from BuddyPress. I landed on this page http://buddypress.org/community/groups/how-to-and-troubleshooting/forum/topic/hide-certain-blogs-from-the-blog-directory/ where someone suggested to look at your code here. I believed it should work as suggested to change line 7 to if($object!='blogs') //hide for blogs directory only, but it didn't work.
Do you have any idea how to modify your code so it can exclude certain blogs??
Thanks so much!
Hi Corey,
Thank you for the comment.
It won't work for the blog as blogs component does not allow excluding at the moment.
Here is some code that will do it(but will exclude the blogs from blog lists for you as admin too, you may want to use a conditional like is_super_admin)
https://gist.github.com/2790977
hope that helps.
Regards
Brajesh
I'm trying to use this on a multisite and I get it work work on individual site member directories all except for a subdomain like forums.site.com and it has bbpress. Any reason why it works for any other site in the network but just not this one. It has no plugins activated other than bbpress and it has the same child theme the other sites have.
You beauty!!
I have successfully filtered the members directory to remove members with role s2member_level2 – just what I wanted – using your code.
I imagine that to filter free suscribers when using s2Member you'd need to put
$subscribers= get_users( array( โroleโ => โs2member_level0โ ) );
not
$subscribers= get_users( array( โroleโ => โsubscriberโ ) );
In caase it's not clear to anyone how to do this, I added the functions from
https://gist.github.com/2142009
to my functions.php file in my theme.
hi I added the code, and wanted to hide superadmin using 1, but I still see them on the list, does the code still work?
Hi Brajesh,
I need a little add-on to your exclude-users-code: the tab no. of ALL group tabs should show member count minus 1 (minus the admin I'm hiding). Otherwise these Learning groups are confused on info.ch thinking there is a member what they don't haveโฆ Thanks for help!
Hi Brajesh,
Would it be possible for you to add some code to correct the number of members shown in the Group Tab (x minus the hidden one[s])?
It's important for that Learning Group concept I'm using: it's confusing for them to see a number x+1 (I'm hiding the Admin, Admin is of course not a real Member of the Group).
Hi Hans,
you can filter on 'bp_get_total_member_count' to show the correct count.
here is an example.
That will make it work.
Hi Brajesh,
Thanks for your help. While trying to use that code I saw that my Admin account is hidden from the main Members Directory (with the code you supplied earlier) โ but NOT from a Group Members Directory. I don't know why I didn't notice that earlier. Good to have my own Beta-Phaseโฆ ๐
Would you please add that function (also hiding account[s] from Group Members Directory)?
Hi Brajesh I am still newbie with all hooks and filters so I am looking for your help here.
I am using the following code to help me with the search.
current_component == BP_SEARCH_SLUG )//if thids is search page
bp_core_load_template( apply_filters( 'bp_core_template_search_template', 'search-single' ) );//load the single searh template
}
add_action("advance-search","bpmag_show_search_results",1);//highest priority
/* we just need to filter the query and change search_term=The search text*/
function bpmag_show_search_results(){
//filter the ajaxquerystring
add_filter("bp_ajax_querystring","bpmag_global_search_qs",100,2);
}
//show the search results for member*/
function bpmag_show_member_search(){
?>
total_member_count>1):?>
<a href="" >total_member_count),"bpmag");?>
<a href="" >
<a href="" >
<a href="" >
<a href="" >
current_component == BP_SEARCH_SLUG)
return true;
return false;
}
?>
The problem here is when search is done without any search terms it still conduct the search and displays all user. How would I stop from displaying all members? or if it can't be done than I want it to display only friends not all the registered member which are not their friend.
I would also appreciate your help if you can tell me how to disable http://www.example.com/memebers from showing all the users. I don't want my users to see all the user in my site.
This is the code i think the above mentioned code is cutoff.
current_component == BP_SEARCH_SLUG )//if thids is search page
bp_core_load_template( apply_filters( 'bp_core_template_search_template', 'search-single' ) );//load the single searh template
}
add_action("advance-search","bpmag_show_search_results",1);//highest priority
/* we just need to filter the query and change search_term=The search text*/
function bpmag_show_search_results(){
//filter the ajaxquerystring
add_filter("bp_ajax_querystring","bpmag_global_search_qs",100,2);
}
//show the search results for member*/
function bpmag_show_member_search(){
?>
total_member_count>1):?>
<a href="" >total_member_count),"bpmag");?>
<a href="" >
<a href="" >
<a href="" >
<a href="" >
current_component == BP_SEARCH_SLUG)
return true;
return false;
}
?>
how to exclude some type of user role members from member loop ?
Hi Smith,
you will need to find the user ids for that role.
In this case, you can use http://codex.wordpress.org/Function_Reference/get_users to find the ids(note the fields parameter).
Once you get the ids, you can use the above method.
hi thank you for reply me. but in my buddypress site i have two types or role one is 'role a' and second is 'role b' i do not want to show 'role b' type of members in my members page when i writes $user = new WP_User( bp_get_member_user_id() );
if ( $user->roles[0] != 'subscriber' ) :
?> this code in member loop.php then it works but problem is it is hiding the member role users from member loop but it is not showing the no of other roles members limit that i have kept for display users per page. for example i have 7 users from that first three user is of role a and fourth and fifth are of role b, i have kept limit 5 users per page. it is showing only first three users of role a and sixth and seven on other page.
hi please help me how to exclude certain type of user role members from member loop? to display in member page
I have applied your code to my wp, but if press the bp search button with no content or with "admin" as search string, the user still shows up in the search results.
I did a step by step debug of your code, everything seems to go right, $object contains 'members' and the contents of $qs that is returned are 'Array ( [exclude] => 1 )'.
And thats where I lose track of this problem
Hello.
I have wordpress version 3.4.2 – buddypress Version 1.6.1 & 3rd party buddypress theme installed.
Can you assist me in what exact code I would need to 'hide' admin user from BuddyPress? I am using the Global Hide Admin bar plugin, so that's not the problem.
I would like user 1 (admin) to be in stealth mode with no traces of that profile inside buddypress registered/unregistered.
Thanks very much!
Hi Brandon,
Though you can easily hide admin from the directory(using above code) and make him inactive by deleting the last_active time , it is not feasible to hide him from Groups at the moment. I have worked with Hans on the similar problem, and we faced a lot of issues.
If you don't want the admin profile to be visible for anyone, just use some check on init action and redirect user if they try to visit the admin account.
Hi, @sbrajesh, i dont know whats wrong with the code above because it hide admin in member directory and also on friends list
Hi, @sbrajesh, i dont know whats wrong with the code above because it hide admin in member directory and also on friends list when you click on friends tab.
Hi Brajesh,
How can I exclude the current loggedin user from members directory and search?
Thanks
Hi Sara,
Thank you for asking.
You can use the code below to exclude the current logged in user
hope that helps.
Pingback:Beginner’s Guide To BuddyPress: Tips And Resources
Can you provided an updated version of the code, where the excluded user isn't seen even if he/she is friends (so it'll be almost impossible to friend him/her) and so that the excluded user isn't seen even if the user looking for the excluded user is not logged in?
I don't think this code works if the user looking for the excluded user is not logged in based off of this comment (//hide for members only), but I may be misinterpreting it.
Hi John
Just remove these two line from the code
and the user will be hidden from friend lists too.
hie i put above code right after <?php and finished it with ; but ddnot worked for me..
please help me for this brajesh sir
were exactly to put this code so we can not see friend list of non friends
thanku again sir
Hi Pravesh,
Are you still facing the issue?
How would I exclude 2 user roles? Administrator and Subscriber?
Can this be tweaked to prevent listing any members at all on the members page and the search? I don't want anybody listed, ever, no matter what. I've been scratching my head about how to do it. Thanks!
Hi kenny,
You don't need this in that case.
here is a simple solution
We are returning invalid user id as include causing to hide all members. Can you please try it and let me know if it works for you or not?
how can i hide subscriber+admin from member directory ? I'm using this code
https://gist.github.com/sbrajesh/2142009 to hide subscriber but still admin user shows how to hide them?
HI Dev,
At the moment the get_users function allows only one role. You can call it twice and use the array_merge to build the list of excluded users.
If you need the complete code, please do ping me again ๐
hmm thanks for your reply ๐ i do need the full code again
Hi Dev,
here is the code for you
https://gist.github.com/sbrajesh/5904292
hope that helps ๐
๐
Hi Brajesh, just passing by to say thank you very much! That was the solution i needed! Grettings from Brasil! You're a great contributer to the developer's society!
Btw, is there any possible way to implement the same code in the BP widgets? Like Who's Online, don't show the excluded users? Ty
Hi Willim,
The above method won't work for Widgets but there is a way to filter users(the method filters users from everywhere).
I will post the code tomorrow after a little experiment.
Thanks
Brajesh
Hi William,
Thank you. Appreciate you comment ๐
I am sorry for being so bunt and un-informed but I am absolute beginner.
While creating my wp blog and buddypress I had my email address as username, and now it is visible to everyone and can not be changed (as far as my knowledge goes) . I want to use your code to at least hide me as admin from buddypress, but I don't know what this is "We will need to hook to 'bp_ajax_querystring' filter."
where do I hook to, and how to get to there?
Thank you
Hi Dijana,
First of all, you can change your username.
Here is another plugin that allows you to change your username
https://buddydev.com/plugins/bp-username-changer/
Please use that to change your username.
Hope that helps.
members by pmpro subscription x level ? pmpro comes connected alot with buddypress nowadays and i want to hide non paying members from the members page currently it shows all buddypress members
Hi RP,
Thank you. I haven't used PMPro recently, but it can be certainly done. Will look in next few weeks and update here. I am sorry, but due to other priorities, won't be able to look into it this week.
Hi Brajesh,
Got the PMpro one figured out or i think maybe i do, created a bp-custom.php in the plugins folder
(code Below)
Now i need to figure out how to exclude them from the next member pagination in the buddypress profile page any advice on that would be great as well ๐ hunting the code down now and theres a tid bit in the themes kleo/buddypress/buddypress-functions.php that looks promising (code below) let me know what you think i want to make sure non paying members are not shown in members directory search and navi ๐ our non paying members are families and children and its important that only the paid members/identity verified are shown.
oh and just read your post reply below should i be using that implementation instead?
Thanks
RP
Hi Braj,
Thanks for your help here. Please, I have 3 administrators on my site. Is there a way to hide the 3 of them?
Hi Learner,
here I have posted the updated tutorial with an example code that excludes all admin users from all the lists.
https://buddydev.com/buddypress/hiding-users-on-buddypress-based-site/
Hope that gives you better opportunity to learn ๐
Wow. Thanks a lot for the quick response, Braj. The code worked! Plus, that was surely a great opportunity to learn ๐ Thanks to you (y)
Hey, this really does sound like something I am looking for.
We have a membership site as a Learning Plattform (with LearnDash integrated).
Now the thing is that since the whole GDPR thing, users expect us to give them a choice weather or not they want only to access the course, being invisible to other users, OR if they want to be also part of the BuddyPress Group.
So the question comes up, how we can realize this. I found this post up here, which sounds promising. What we are mainly looking for is a solution the user can decide by pushing a button which will change if he:
– has access to all BuddyPress content
– will be listed in the /members directory or not
Do you have any idea how this could be done?
Looking very much forward to any answers and ideas…
Thx, Arne
Hi Arne,
It seems to me one of our plugins might help you specifically with user listing in directory and search.
https://buddydev.com/plugins/bp-profile-visibility-manager/
Regards
Brajesh