Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Hide Subscriber Users from BuddyPress member Directory
add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
//list of users to exclude
if($object!='members')//hide for members only
return $qs;
$excluded_user=join(',',bpdev_get_subscriber_user_ids());//comma separated ids of users whom you want to exclude
$args=wp_parse_args($qs);
//check if we are searching for friends list etc?, 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;
}
function bpdev_get_subscriber_user_ids(){
$subscribers= get_users( array( 'role' => 'subscriber', 'fields' => 'ID' ) );
return $users;
}
@jibbius

This comment has been minimized.

Show comment Hide comment
@jibbius

jibbius Mar 31, 2013

You can replace the entire bpdev_get_subscriber_user_ids() function, with this single line of code:

$excluded_user = implode(',',get_users('role=subscriber&fields=ID'));

jibbius commented Mar 31, 2013

You can replace the entire bpdev_get_subscriber_user_ids() function, with this single line of code:

$excluded_user = implode(',',get_users('role=subscriber&fields=ID'));
@virtualgeorge

This comment has been minimized.

Show comment Hide comment
@virtualgeorge

virtualgeorge Mar 17, 2014

What if you wanted to hide Administrators as well as Subscribers?

What if you wanted to hide Administrators as well as Subscribers?

@marriageagency

This comment has been minimized.

Show comment Hide comment
@marriageagency

marriageagency Jul 28, 2014

I want to Hide Subscriber Users from BuddyPress member Directory. Where exactly should I create the hide-subscribers.php file, in which folder? I'm a novice at this so any details that you can provide to help me with this will be greatly appreciated. Thank you.

I want to Hide Subscriber Users from BuddyPress member Directory. Where exactly should I create the hide-subscribers.php file, in which folder? I'm a novice at this so any details that you can provide to help me with this will be greatly appreciated. Thank you.

@inxys

This comment has been minimized.

Show comment Hide comment
@inxys

inxys Feb 4, 2015

Please explain bit more i want hide the subscriber user role in buddypress member list, then i want to know how to use this hook code used in another plugin

inxys commented Feb 4, 2015

Please explain bit more i want hide the subscriber user role in buddypress member list, then i want to know how to use this hook code used in another plugin

@hannah-wright

This comment has been minimized.

Show comment Hide comment
@hannah-wright

hannah-wright May 10, 2015

inxys: in case you're still wondering, you'll want to paste the code in "custom_buddypress/bp-functions.php"

inxys: in case you're still wondering, you'll want to paste the code in "custom_buddypress/bp-functions.php"

@sbrajesh

This comment has been minimized.

Show comment Hide comment
@sbrajesh

sbrajesh Aug 27, 2015

I have updated the code. Please be warned that it had serious performance issues due to an oversight. If you are using it, Update the code.

Owner

sbrajesh commented Aug 27, 2015

I have updated the code. Please be warned that it had serious performance issues due to an oversight. If you are using it, Update the code.

@rpandassociates

This comment has been minimized.

Show comment Hide comment
@rpandassociates

rpandassociates Oct 7, 2015

so this goes in members loops ?
I want to show only members that have a certain pmpro level

so this goes in members loops ?
I want to show only members that have a certain pmpro level

@fscbmwcca

This comment has been minimized.

Show comment Hide comment
@fscbmwcca

fscbmwcca Jan 4, 2016

Hi, I'm a newbie and know very little php.
I want to hide buddypress to all subscribers. As I am reading this you have to enter the subscriber ids as indicated //comma separated ids of users whom you want to exclude. Is there any way to indicate all subscribers and not enter the ids? I am going to allow subscribers to sign up as general public and do not want them to have access to buddypress or bbpress. Non-subscribers will have access to buddypress and bbpress. Is this possible to do?
Any assistance would be appreciated.
Thanks

Hi, I'm a newbie and know very little php.
I want to hide buddypress to all subscribers. As I am reading this you have to enter the subscriber ids as indicated //comma separated ids of users whom you want to exclude. Is there any way to indicate all subscribers and not enter the ids? I am going to allow subscribers to sign up as general public and do not want them to have access to buddypress or bbpress. Non-subscribers will have access to buddypress and bbpress. Is this possible to do?
Any assistance would be appreciated.
Thanks

@unuigbee

This comment has been minimized.

Show comment Hide comment
@unuigbee

unuigbee Feb 27, 2016

Hi sbrajesh,

There is a mistake on line 33. It should be return $subscribers not return $users

Also if you need to add multiple roles you can do this (You need to have at least WordPress Version 4.4 for this to work!):

function bpdev_get_user_ids(){
  $users = get_users( array( 'role__in' => ['customer', 'subscriber'], 'fields' => 'ID' ) );
  return $users;
}

You can put sbrajesh code in bp-custom.php in the plugins folder. See here for more insight: https://codex.buddypress.org/themes/bp-custom-php

Hi sbrajesh,

There is a mistake on line 33. It should be return $subscribers not return $users

Also if you need to add multiple roles you can do this (You need to have at least WordPress Version 4.4 for this to work!):

function bpdev_get_user_ids(){
  $users = get_users( array( 'role__in' => ['customer', 'subscriber'], 'fields' => 'ID' ) );
  return $users;
}

You can put sbrajesh code in bp-custom.php in the plugins folder. See here for more insight: https://codex.buddypress.org/themes/bp-custom-php

@wdbayless

This comment has been minimized.

Show comment Hide comment
@wdbayless

wdbayless Mar 18, 2016

Thanks, sbrajesh, for this. It's exactly what I needed!

Thanks, unuigbee for catching this:

There is a mistake on line 33. It should be return $subscribers not return $users

As well as your suggestion for excluding multiple member types.

Thanks, sbrajesh, for this. It's exactly what I needed!

Thanks, unuigbee for catching this:

There is a mistake on line 33. It should be return $subscribers not return $users

As well as your suggestion for excluding multiple member types.

@majecdad

This comment has been minimized.

Show comment Hide comment
@majecdad

majecdad Jun 1, 2016

Thank you sbrajesh for sharing this code, and unuigbee for the adjustments for multiple roles. It now works awesome. However, it needed a mod that I literally spent half day trying to figure out (which just proves how green I am sometimes) :) so I post this for the next 'copy and paste' coder who is looking for this fix.

I could not get it to work until I changed function bpdev_get_user_ids() in the multiple roles part to
function bpdev_get_subscriber_user_ids() which then matched what was in the original code.

The multiple roles thing was huge for me, so I really appreciate that. I'm really not sure if I should have changed the other part instead (removing the word subscriber), but once I got it to work, I didn't dare touch it again. :) If it's a big deal and I should change it, please let me know.

Thanks all.

majecdad commented Jun 1, 2016

Thank you sbrajesh for sharing this code, and unuigbee for the adjustments for multiple roles. It now works awesome. However, it needed a mod that I literally spent half day trying to figure out (which just proves how green I am sometimes) :) so I post this for the next 'copy and paste' coder who is looking for this fix.

I could not get it to work until I changed function bpdev_get_user_ids() in the multiple roles part to
function bpdev_get_subscriber_user_ids() which then matched what was in the original code.

The multiple roles thing was huge for me, so I really appreciate that. I'm really not sure if I should have changed the other part instead (removing the word subscriber), but once I got it to work, I didn't dare touch it again. :) If it's a big deal and I should change it, please let me know.

Thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment