Using BuddyPress Member Types as Profile Field Visibility Levels
Recently, I was asked about using BuddyPress Member Types as profile field visibility level. It seemed like a good question to me and I am going to share the code to make it work.
Before we start, The goal of visibility is to set the visibility of the profile field data for other users not the profile owner. You are allowing profile owners to choose who can see their data. If you want to hide certain fields from user for editing purpose, you should use the "Member Types" option on the profile field add/edit screen.
Now, that we are clear about visibility, let us explore the possibility. If you don't know how to extend visibilities, you may want to take a look at my post Extending BuddyPress Profile Field Visibility.
Step 1:- Adding BuddyPress member type names as visibility level
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 | /** * Add extra visibility levels. * * @param array $visibilities visibilities. * * @return array */ function buddydev_add_member_type_profile_visibility( $visibilities ) { $member_types = bp_get_member_types( array(), 'object' ); // If there is no registered member type, return the original visibilities. if ( empty( $member_types ) ) { return $visibilities; } // Add our member type as visibility levels. foreach ( $member_types as $member_type => $member_type_object ) { $visibilities[ $member_type ] = array( 'id' => $member_type, 'label' => $member_type_object->labels['singular_name'], ); } return $visibilities; } |
As soon as we are done with it, the new levels will be available to select from the list of visibility as shown below.
That looks good but we are not done yet. It only lists the member types as visibilities and lets user select them.
Step 2:- Hiding fields from other users based on the field visibility
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 | /** * Hide profile fields based on member type visibility. * * @param array $hidden_levels hidden visibility levels. * @param int $displayed_user_id displayed user id. * @param int $current_user_id logged in user id. * * @return array */ function buddydev_get_hidden_visibility_types_for_user( $hidden_levels, $displayed_user_id, $current_user_id ) { $member_types = bp_get_member_types(); // If user is not logged in // All member type visibilities will be hidden. if ( ! is_user_logged_in() ) { $hidden_levels = array_merge( $hidden_levels, $member_types ); } elseif ( ( $displayed_user_id == $current_user_id ) || is_super_admin( $current_user_id ) ) { // Do not hide anything on own profile or viewed by super admin. } else { // User is logged in and viewing other user's profile. $logged_member_types = bp_get_member_type( $current_user_id, false ); // except current user's member type, all other levels will be hidden. if ( $logged_member_types ) { $member_types = array_diff( $member_types, $logged_member_types ); } $hidden_levels = array_merge( $hidden_levels, $member_types ); } return $hidden_levels; } add_filter( 'bp_xprofile_get_hidden_field_types_for_user', 'buddydev_get_hidden_visibility_types_for_user', 10, 3 ); |
And it will hide the profile field based on member type as visibility level.
Have fun.