Skip to content

Instantly share code, notes, and snippets.

@KaineLabs
Created April 23, 2019 20:48
Show Gist options
  • Save KaineLabs/cec010e3aad328a4dabc73415a986d58 to your computer and use it in GitHub Desktop.
Save KaineLabs/cec010e3aad328a4dabc73415a986d58 to your computer and use it in GitHub Desktop.
Exclude Users from BuddyPress Members List by WordPress role.
<?php
/**
* Exclude Users from BuddyPress Members List by WordPress role.
*
* @param array $args args.
*
* @return array
*/
function yzc_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', 'yzc_exclude_users_by_role' );
@mohamettarik
Copy link

mohamettarik commented Sep 3, 2021

“Edited comment”

Hello and thanks for great effort being done by you.

I added it to bp-cutom.php, unfortunately it did not work and the script appeared as text on top of site header.

Then cleared bp-cutom.php and tried to add it as snippet it worked well and happy for result.

i have a extra question, is there a way to strict the excluded roles from activity, posting and having BuddyPress profiles ?

I understand surely your are busy and appreciating your response.

@KaineLabs
Copy link
Author

KaineLabs commented Sep 3, 2021

Hello Mohammed,

Hope you are doing great.

For the bp custom.php make sure to add at the beginning of the file this line below, but if you wanna try this solution make sure to disable the snippet you created first as duplicated code will result in site going down.

<?php
For Restricting Pages and posting and other Youzify features by role, I recommend using our addon Membership Restrictions:

https://youzify.com/downloads/buddypress-membership-restrictions/

For disabling profiles by the role you can use this:

https://gist.github.com/KaineLabs/47523bad96fe9536c80611e4f74aca7b

Hope this helps.

Best Regards, Youssef.

@JurekOW
Copy link

JurekOW commented Dec 28, 2023

Hello,

Is it possible to hide more than one role with this snippet? I tried to hide 'administrator' and 'customer'. it hides the admins but not the customer. Did i something wrong?

$role = 'administrator';'customer';// change to the role to be excluded.
$user_ids = get_users( array( 'role' => $role, 'fields' => 'ID' ) );

Than you for your help!

best regards Jurek

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