Last active
May 22, 2024 11:37
-
-
Save sbrajesh/2142009 to your computer and use it in GitHub Desktop.
Hide Subscriber Users from BuddyPress member Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
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.
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
Super late reply, but for @Hastibe and anyone else who wants to exclude all but one role, change the exclude to include.