BuddyDev

Search

Display Member Type Label in Directory

Tagged: ,

  • Participant
    Level: Initiated
    Posts: 2
    Darrin on #11054

    Apologies if this is addressed elsewhere already.

    Regarding the Member Type plugin for BuddyPress I am looking for guidance on how to display the member type as a label next to the user name in both the Member Directory and on the Member’s Profile page.

    Is there a specific function to insert into my templates?

    Thanks in advance.

    Darrin

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #11057

    Hello Darrin,

    Thank you for posting here. I have question what type of member type you are using.
    1. Is it single member type per user?.
    2. Is it multiple member types to single user?.

    Please let me know.

    Thank you
    Ravi

  • Participant
    Level: Initiated
    Posts: 2
    Darrin on #11071

    Hi Ravi,

    I am using a single member type right now, but would be interested in the solutions for both options.

    Thanks,

    Darrin

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #11111

    Hi Darrin,
    Ravi is on leave, I am going to help you with this.

    Please put the following code in your bp-custom.php

    
    
    /**
     * Print the list of member types of the user in member directory.
     */
    function buddydev_show_member_type_labels() {
    	$user_id = bp_get_member_user_id();
    	if( ! $user_id ) {
    		return ;// It should never happen but in case someone put the ac
    	}
    
    	$member_types = bp_get_member_type( $user_id,  false );
    	if ( ! $member_types ) {
    		return;
    	}
    
    	$labels = array();
    	foreach ( $member_types as $member_type ) {
    		$mtype_object = bp_get_member_type_object( $member_type );
    		if ( ! $mtype_object ) {
    			continue;
    		}
    
    		$labels[] = $mtype_object->labels['singular_name'];
    	}
    
    	echo join( ', ', $labels );
    }
    add_action( 'bp_directory_members_item', 'buddydev_show_member_type_labels' );
    

    It will work for single/multiple both.

    You can modify it as you please.

    Hope that helps.
    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Darrin on #11116

    Hey Brajesh,

    Thanks for the quick response. This worked great in the Members Directory (members-loop.php), but it is not displaying the label on the member’s profile page. Any ideas?

    Darrin

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #11193

    We will need to rewrite that. I will break it into two functions and post back today.

    Thank you
    brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #11267

    Hi Darrin,
    Please remove the old code and use this. It shows for both the displayed user as well as the directory item.

    
    /**
     * Get an array of member type labels based on member types.
     *
     * @param array $member_types member types.
     *
     * @return array
     */
    function buddydev_get_member_type_labels( $member_types ) {
    
    	$labels = array();
    	if ( ! $member_types ) {
    		return $labels;
    	}
    	foreach ( $member_types as $member_type ) {
    		$mtype_object = bp_get_member_type_object( $member_type );
    		if ( ! $mtype_object ) {
    			continue;
    		}
    
    		$labels[] = $mtype_object->labels['singular_name'];
    	}
    
    	return $labels;
    }
    
    /**
     * Print the list of member types of the user in member directory.
     */
    function buddydev_show_member_type_labels() {
    	$user_id = bp_get_member_user_id();
    
    	if ( ! $user_id ) {
    		return;// It should never happen but in case someone put the ac
    	}
    
    	$labels = buddydev_get_member_type_labels( bp_get_member_type( $user_id, false ) );
    
    	echo join( ', ', $labels );
    }
    
    add_action( 'bp_directory_members_item', 'buddydev_show_member_type_labels' );
    
    function buddydev_show_member_type_for_displayed_user() {
    	if ( ! bp_is_user() ) {
    		return; // just a safeguard, it should never happen.
    	}
    
    	$user_id = bp_displayed_user_id();
    	$labels  = buddydev_get_member_type_labels( bp_get_member_type( $user_id, false ) );
    
    	echo join( ', ', $labels );
    }
    
    add_action( 'bp_member_header_actions', 'buddydev_show_member_type_for_displayed_user' );
    
    

    Hope that helps.
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved