Hiding Users on BuddyPress based site
Do you want to hide users from directories/widgets/friend lists etc on a BuddyPress site? I had covered it 3 years ago in another post here. Since then, things have changed. Though the old code is still working, there are better ways now to exclude users. Instead of filtering on "bp_ajax_querystring", you can filter on "bp_before_has_members_parse_args" or "bp_after_has_members_parse_args".
Here is a general example on how to exclude 3 users with IDS 1,2,3 from all the list(yes, It will remove from widgets/friend lists too).
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 | /** * Exclude users from BuddyPress members list. * * @param array $args args. * * @return array */ function buddydev_exclude_users( $args ) { // do not exclude in admin. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return $args; } $excluded = isset( $args['exclude'] ) ? $args['exclude'] : array(); if ( ! is_array( $excluded ) ) { $excluded = explode( ',', $excluded ); } // Change it with the actual numeric user ids. $user_ids = array( 1, 2, 3 ); // user ids to exclude. $excluded = array_merge( $excluded, $user_ids ); $args['exclude'] = $excluded; return $args; } add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users' ); |
If you want to exclude all users by a specific WordPress roles from listing on your BuddyPress site, that is easy. Here is a code example showing how to exclude all admins from every list on the site.
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 | /** * Exclude Users from BuddyPress Members List by WordPress role. * * @param array $args args. * * @return array */ function buddydev_exclude_users_by_role( $args ) { // do not exclude in admin. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { return $args; } $excluded = isset( $args['exclude'] ) ? $args['exclude'] : array(); if ( ! is_array( $excluded ) ) { $excluded = explode( ',', $excluded ); } $role = 'administrator';// change to the role to be excluded. $user_ids = get_users( array( 'role' => $role, 'fields' => 'ID' ) ); $excluded = array_merge( $excluded, $user_ids ); $args['exclude'] = $excluded; return $args; } add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users_by_role' ); |
You can put the above code in your bp-custom.php or your theme's functions.php. I prefer to keep things in bp-custom.php
Though these approaches work, there is even better way to exclude large number of users by filtering sql query. If you are using our BuddyPress Profile Visibility Manager plugin, it uses the sql queries filtering approach. Also, the BuddyPress Profile Visibility Manager plugin gives a lot of flexibility for user level privacy on BuddyPress site. In case you want to give your users more privacy control for their BuddyPress profile, you may want to check this plugin.
Hope this post helps you all looking for a simple solution to exclude users on BuddyPress site.
Very useful! =)
Thank you Renato 🙂
Hi, thanks a lot! works perfect 🙂
what do i have to add/ write when i want to change more than one role?
Can your code me modified to hide users based on the member's first name, last name or full name?
Please respond soon.
Great tips Brajesh! Exactly what I was looking for to hide members from widgets too. Between this and the snippet in your old post I have the flexibility to hide members where I see fit. Thanks!
Thank you Mark. It's a pleasure to know it helped 🙂
Hi,
i include bb-custom.php with your function but i still see admin account!
Hi Wael,
Please put it in bp-custom.php not bb-custom.php
Hope that helps.
Hello Brajesh,
This is really nice, its working fine except I see the user in the "who's online" widget. I don't if the who's online can be bypassed also.
Thank you!
Hi Javier,
Thank you. Please recheck if the user is properly excluded. If it is not excluding from online members widget that will be a bug. The standard BuddyPress who's online widget uses bp_has_members() and should exclude.
Finally found a working script for our bp instance! thanks for this little piece of code!
Thank you Michael 🙂
Hi again, Brajesh Singh.
This doesn't seem to work. Any idea that others had said, (with the same experience) as to why it doesn't work for me?
Ah! It's working! Alas, it HAD to go into my child theme functions, not where I thought it should:
wp-content/themes/child-theme/buddypress/bp-custom.php
Brajesh Singh, thank you, anyway!!
Thank you Andrian. I am glad that you found it . The bp-custom.php is kept in wp-content/plugins directory. But the code can go into theme's functions.php too 🙂
Hi Brajesh, this is awesome. Thank you.
Is it possible though that you can make a code that does the following:
1. Hide all admin from EVERYONE
2. Hide members from non logged in users (my site is for members only)
3. Still make members searchable to logged in members – but admin remain hidden?
Hi Kabelo,
Thank you. One of these days, when I get some time, I will post again with the suggestion.
I just changed out your previous code for this update, thank you. I too had a question about excluding multiple roles.
I tried messing around with structure in
to add another role, or an array, but could not get it to work.
Ultimately, I removed the $role = 'administrator';//change to the role to be excluded
and changed the other line to:
some of which I had grabbed from the change to your old code. To my complete surprise, it worked. 🙂
But is there a more 'correct' way to do multiple roles?
Thank you.
Hi Mitch,
You are doing it correctly.
Great but how to implement this? Is it custom css? Can this be made into a plugin for those of us who do not know what we are doing? thanx 🙂
HI Derek,
I am sorry. This needs to be put in bp-custom.php file. Since we already have a pro plugin that does this and a lot more, we decided to not do it as a plugin.
Hi Brajesh,
Please is bp-custom.php to be created or already existing in a directory? If already existing where can it be found, please? If not where is it to be placed when created? Thanks
Hi Tayo,
It is in the wp-content/plugins directory.
If it is not there, you may need to create one
For more details, Please see this
https://buddydev.com/docs/guides/guides/buddypress-guides/what-is-bp-custom-php/
Thanks Man!! Helped a lot
You are welcome Olaf 🙂
Thanks a lot for this simply hack…!
Just one question…
If I search (in my developement site) ie. Users Directory for all users, the admin is invisible, but the counter is wrong, so the admin account is invisible, but were still counted.
Is it possible to fix this?
Thanks for any thoughts on this…
Hi Sven,
Thank you for the comment. It is doable but doing an extra queries did not seem a great idea to me and that's why I did not post it. Please open a topic on our forum and I will assist with the code.