Tagged: roles filtering
Hi
I am running a membership site. When membership expires, the member is demoted to a ‘subscriber’ role. They should then be excluded from BuddyPress listings and searches. I had this working ok before the new BuddyPress but it seems a bit trickier with Nouveau. I have the following code so far in bp-custom.php.
add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users_by_role' ); add_filter( 'bp_members_suggestions_query_args', 'buddydev_exclude_users_by_role' ); 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 ); } $user_ids = get_users( array( 'role__in' => ['subscriber'], 'fields' => 'ID' ) ); $excluded = array_merge( $excluded, $user_ids ); $args['exclude'] = $excluded; return $args; }
The first filter seems to fix the regular member listing.
The second filter fixes the autocomplete when trying to find a user for messages.Do you know how I can filter the list for inviting members to groups?
Is there a better way to do this globally for BuddyPress?
Thank you for your help.
Hi Jemima,
Welcome to BuddyDev and thank you for the question.Which version of BuddyPress are you using? Please let me know and I will assist.
Regards
BrajeshHi Jemima,
I am sorry for the delayed reply. I wanted to take a look at the invite code before replying.To achieve this in nouveau is not simple. The developers have made sure to use every bad trick available in the book.
The seems to be only possible strategy that you add an ajax handler which has higher priority and hook to ‘wp_ajax_groups_get_group_potential_invites’
The handler needs to copy code from the function ‘bp_nouveau_ajax_get_users_to_invite’ and exclude users there.
It seems like a not so well thought implementation for now.
Regards
BrajeshHi Brajesh,
Thanks very much for your reply. I’m glad it’s not obvious, at least!
I have found another way, as follows. Does this look good to you? From what I can tell, it’s ok, but there might be something obviously wrong with it that your experience will quickly spot?
add_action( 'bp_pre_user_query_construct', 'wd_build_exclude_args', 20, 1 ); function wd_build_exclude_args($args) { $excluded = isset( $args->exclude ) ? $args->exclude : array(); $user_ids = get_users( array( 'role__in' => ['subscriber'], 'fields' => 'ID' ) ); $excluded = array_merge( $excluded, $user_ids ); $args->query_vars['exclude'] = $excluded; return $args; }
Thank you!
Jemima
Hi Jemima,
That will work. It will apply to all members loops though.Also, Here is a minor enhancement(the above code did not respect already excluded users)
add_action( 'bp_pre_user_query_construct', 'wd_build_exclude_args', 20, 1 ); function wd_build_exclude_args( $args ) { $excluded = isset( $args->query_vars['exclude'] ) ? $args->query_vars['exclude'] : array(); $user_ids = get_users( array( 'role__in' => [ 'subscriber' ], 'fields' => 'ID' ) ); $excluded = array_merge( $excluded, $user_ids ); $args->query_vars['exclude'] = $excluded; return $args; }
Regards
Brajesh
You must be logged in to reply to this topic.