BuddyDev

Search

[Resolved] Adding User Role or a member type as new visibilty level

  • Participant
    Level: Initiated
    Posts: 4
    Michael Deinhardt on #17907

    We have recently bought Member Types Pro and would like to add either a member type or a user role as a new visibility level. I have seen this article https://buddydev.com/extending-buddypress-profile-field-visibility/ but can’t figure out how to do it. Can you help us achieve this?

    Rgsd,

    Michael

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #17917

    Hi Michael,
    Welcome to BuddyDev forums.

    Thank you for purchasing member types pro.

    I will post the code on Monday to show some example for visibility.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Michael Deinhardt on #17985
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #17988

    Hi Michael,
    I have posted a blog showing how to do it here.

    https://buddydev.com/using-buddypress-member-types-as-profile-field-visibility-levels/

    I will add the code to our BuddyPress Member Types Pro plugin in next release with a few other enhancements too.

    For the other thinsg, Please send us the requirements from here
    https://buddydev.com/hire-us/

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Michael Deinhardt on #18057

    Hi Brajesh,

    Thans you very nmuch for the code and the blog post. Finally I got around to test this, but am failing 🙁

    The only thing I need to change in the first code, i.e. Step 1 is the line
    'label' => $member_type_object->labels['singular_name'],
    correct?

    I replaced “singular_name” with the Singular Label of the respective Member Type, wich is “Recruiter”, so my whole function in functions.php is now:

    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['Recruiter'],
            );
        }
     
        return $visibilities;
    }

    But I still don’t see the new visibility level in the users profiles 🙁
    What am I missing?

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #18061

    Hi Michael,
    I am sorry for the confusion.

    You don’t need to change anything in the code. It is not working as it is unable to find the labels[‘Recruiter’]

    Is your goal to limit the available visibilities to only certain member types(like only Recruiter is available as a visibility level?)

    The code in the blog post will automatically take the singular label for all member types.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Michael Deinhardt on #18062

    Hi Brajesh,

    >>Is your goal to limit the available visibilities to only certain member types(like only Recruiter is available as a visibility level?)

    Yes, exactly. Sorry for being unclear about this. I did try the code literally though, I had a hunch that I was wrong, but it still didn’t work 🙁

    Cheers,
    Michael

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #18068

    Hi Michael,
    No Problem. Let us change the code a bit.

    Step 1:- Please remove the old code for member type visibility and use the following

    
    
    /**
     * Get an array of member types enable for visibility.
     *
     * @return array
     */
    function buddydev_get_visibility_member_types() {
    
    	return array(
    		'recruiter',
    		'staff',
    	);
    }
    
    /**
     * 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;
    	}
    
    	$allowed_member_types = buddydev_get_visibility_member_types();
    
    	// Add our member type as visibility levels.
    	foreach ( $member_types as $member_type => $member_type_object ) {
    
    		if ( ! in_array( $member_type, $allowed_member_types ) ) {
    			continue;
    		}
    
    		$visibilities[ $member_type ] = array(
    			'id'    => $member_type,
    			'label' => $member_type_object->labels['singular_name'],
    		);
    	}
    
    	return $visibilities;
    }
    
    add_filter( 'bp_xprofile_get_visibility_levels', 'buddydev_add_member_type_profile_visibility' );
    
    /**
     * 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 ) {
    
    	// Consider all listed member types as invisible.
    	$member_types = buddydev_get_visibility_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 );
    
    

    Now, in order to limit to only certain member types, all you have to do is change this

    
    
    	return array(
    		'recruiter',
    		'staff',
    	);
    
    

    Change the values in this array with the actual member type name(unique name).

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Michael Deinhardt on #18084

    Hi Brajesh,

    a w e s o m e. Thank you so very much. This works perfectly and I can’t thank you enough for taking the time to “whip up” that code.

    And I think/hope this makes a fine addition for others using Member Types Pro as well.

    Thanks again

    Michael

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #18087

    Thank you for the kind words Michael.
    I am glad I could assist. Thank you too as I too believe that it will help others.

    Best Regards
    Brajesh

The topic ‘ [Resolved] Adding User Role or a member type as new visibilty level’ is closed to new replies.

This topic is: resolved