Show only users of opposite genders on BuddyPress site
If you are creating a dating site or something that needs you to filter users list based on current user's gender, It may not be vary obvious in BuddyPress. Believe me , It is easier than what you might think.
Basic Premise:-
BuddyPress does not have a way to specify user gender's. We can either use Profile field(dropdown/radio) or member type. I will use member type for the genders. The benefit of using member type is, it allows efficient filtering of the users.
Assumptions:-
I am assuming you have two member types 'male' and 'female' registered. Any users with male member type will be considered as male and the same goes for the female member type. I am also considering male member type as opposite of the female member type for the sake of our use case.
Goals:-
- We will filter members listings and only show users of opposite gender if a user is logged in, and has the gender set.
- When a user of same gender tries to access other user's profile, we deny the access.
- We do not do anything if a user is not logged in.
Code:-
We will write a few utility functions that will help us with our implementation. Here is the list of functions we plan to use.
- is_user_male( $user_id ) : bool
- is_user_female( $user_id ): bool
- get_user_gender( $user_id ) : string ( 'male' or 'female' )
- get_opposite_gender( $user_id ): string ( 'male' or 'female' )
That's our list of utility functions. I have implemented these as class methods.
Here is the concrete implementation of the whole filtering of BuddyPress users.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /** * Filters Users based on Gender. Make sure only users of opposite genders are visible. */ class BuddyDev_Gender_Based_Member_Filter { /** * Boot * * @return BuddyDev_Gender_Based_Member_Filter */ public static function boot() { $self = new self(); $self->setup(); return $self; } /** * Setup hooks. */ private function setup() { add_filter( 'bp_after_has_members_parse_args', array( $this, 'filter_args' ) ); add_action( 'bp_template_redirect', array( $this, 'check_access' ), 10, 2 ); } /** * Modifies members loop args to filter out users of same gender. * * @param array $args Array of group allowed parameters. * * @return array */ public function filter_args( $args ) { // do not filter if user is not logged in, or is super admin or we are inside dashboard. if ( ! is_user_logged_in() || is_super_admin() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) { return $args; } $opposite_gender = $this->get_opposite_gender( bp_loggedin_user_id() ); if ( $opposite_gender ) { $args['member_type'] = $opposite_gender; } return $args; } /** * Restricts access to user profile. * * Only allows profile owner, user of opposite gender or site admins to view. */ public function check_access() { if ( ! is_user_logged_in() || ! bp_is_user() || bp_is_my_profile() || is_super_admin() ) { return; } $gender = $this->get_user_gender( bp_loggedin_user_id() ); if ( ! $gender ) { return; } // do not allow access for same gender. if ( bp_has_member_type( bp_displayed_user_id(), $gender ) ) { bp_core_add_message( __( 'Access denied.', 'yym-helper' ) ); bp_core_redirect( bp_loggedin_user_domain() ); } } /** * Returns user Gender. * * @param int $user_id user id. * * @return string|null */ private function get_user_gender( $user_id ) { $gender = null; $member_types = bp_get_member_type( $user_id, false ); if ( empty( $member_types ) ) { $gender = null; } elseif ( $this->is_user_male( $user_id ) ) { $gender = 'male'; } elseif ( $this->is_user_female( $user_id ) ) { $gender = 'female'; } return $gender; } /** * Finds the opposite gender for the given user. * * @param int $user_id user id. * * @return string|null */ private function get_opposite_gender( $user_id ) { $user_gender = $this->get_user_gender( $user_id ); if ( ! $user_gender ) { return null; } if ( 'male' === $user_gender ) { return 'female'; } elseif ( 'female' === $user_gender ) { return 'male'; } return null; } /*** * Checks if user is male or not. * * @param int $user_id User id to check. * * @return bool */ private function is_user_male( $user_id ) { return bp_has_member_type( $user_id, 'male' ); } /*** * Checks if user is female or not. * * @param int $user_id User id to check. * * @return bool */ private function is_user_female( $user_id ) { return bp_has_member_type( $user_id, 'female' ); } } BuddyDev_Gender_Based_Member_Filter::boot(); |
Please put this code in your bp-custom.php and you will be good to go.
Resources:-
- If you need help creating member types, you may use our free BuddyPress Member Types Generator or our paid solution BuddyPress Member Types Pro.
- To allow users select their member type, you can use Member Type field. If using BuddyPress Member Type Generator plugin, you can download it from here. It comes packed with BuddyPress Member Types pro.