Skip to content

Instantly share code, notes, and snippets.

@sbrajesh
Last active May 22, 2024 11:37
Show Gist options
  • Save sbrajesh/2142009 to your computer and use it in GitHub Desktop.
Save sbrajesh/2142009 to your computer and use it in GitHub Desktop.
Hide Subscriber Users from BuddyPress member Directory
<?php
/**
* Excludes users from BuddyPress Members list.
*
* @param string $qs Query string.
* @param string $object object loop identikfier.
*
* @return string
*/
function bpdev_exclude_users( $qs = false, $object = false ) {
if ( $object != 'members' ) { // hide for members only.
return $qs;
}
// comma separated ids of users whom you want to exclude.
$excluded_users = join( ',', bpdev_get_excluded_user_ids() );
$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_users;
} else {
$args['exclude'] = $excluded_users;
}
$qs = build_query( $args );
return $qs;
}
add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 );
/**
* Retrieves an array of user ids to exclude.
*
* NOTE:- It is not an efficient idea if you want to exclude thousands of users. There are better ways.
*
* @return array
*/
function bpdev_get_excluded_user_ids() {
/*
// Example:- Excluding known user ids
$query = array(1,2,3); // Please use the actual user ids instead of 1,2,3
*/
// Example:- Single role exclusion.
// This excludes all subscribers. Change the role you want to exclude.
$query = array(
'role' => 'subscriber', // you can change the role to any role you want to exclude.
'fields' => 'ID'
);
/*
// Example:- Multiple roles Exclusion
$query = array(
'role__in' => array( 'subscriber, contributor' ), // List of roles to exclude.
'fields' => 'ID'
);
*/
/*
// Example: Limiting to certain roles only.
$query = array(
'role__not_in' => array( 'subscriber, contributor' ), // List of roles to include.
'fields' => 'ID'
);
*/
return get_users( $query );
}
@sbrajesh
Copy link
Author

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
Copy link

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

@fscbmwcca
Copy link

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
Copy link

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
Copy link

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
Copy link

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.

@bryansayler
Copy link

Has anyone made this work by hiding users based on Paid Memberships Pro level?

Ex: hide users with membership level 1,2,3 from BuddyPress Member Directory

@Hastibe
Copy link

Hastibe commented Aug 26, 2020

Would anyone be able to help me with modifying this code so that all user roles are hidden, except for users with a specified user role?

@scottlee
Copy link

Super late reply, but for @Hastibe and anyone else who wants to exclude all but one role, change the exclude to include.

// Helper for fetching all IDs to include. Returns an array in this example.
$included_user_ids = my_function_to_fetch_all_users_by_role();

$args = wp_parse_args( $qs );
$args['include'] = implode( ',', $included_user_ids );

$qs = build_query( $args );

@stephaniewj
Copy link

I am not a developer. Can anyone tell me where to put the user IDs to include in the last post by @scottlee ? For example, if I want to exclude all user roles from the buddypress directory except a user role called "Premium", where do I put that? Can I add more than one user role? Would be great if someone could post a definitive code with an example in it. Thanks in advance to whoever does this.

@sbrajesh
Copy link
Author

I am sorry for not keeping up with the comments here. I have updated the code with examples for multiple roles exclusion. Also, I will recommend using bp_after_has_members_parse_args instead of the bp_ajax_querystring that we have used.
Here is a link to one of my old post about the same.
https://buddydev.com/hiding-users-on-buddypress-based-site/

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