Skip to:
Content
Pages
Categories
Search
Top
Bottom

Show First and Last Name on posts and username when posting in group


  • BexxBl
    Participant

    @bexxihexxi

    The problem:

    I want to display different types of names on the updates in the activity stream like:

    – first name + last name for updates which the user posts on their stream
    – username/display name when there are updates in groups

    i have written the following code:

    /**
     * Get the fullname of the current member in the loop.
     *
     * Full name is, by default, pulled from xprofile's Full Name field.
     * When this field is empty, we try to get an alternative name from the
     * WP users table, in the following order of preference: display_name,
     * user_nicename, user_login.
     *
     * @return string The user's fullname for display.
     */
    function bp_get_member_name_custom() {
    	global $members_template, $bp;
    	// print_r($members_template->member->id);
    	$firstname= bp_get_profile_field_data('field=vorname&user_id='.$members_template->member->id);
    	$lastname= bp_get_profile_field_data('field=nachname&user_id='.$members_template->member->id);		
    	if(!empty($firstname) || !empty($firstname)){
    		$fullname = $firstname.' '.$lastname ;
    		return apply_filters( 'bp_get_member_name_custom',$fullname );
    	}else{
    		$name_stack = array(
    			'display_name',
    			'user_nicename',
    			'user_login'
    		);
    
    		foreach ( $name_stack as $source ) {
    			if ( !empty( $members_template->member->{$source} ) ) {
    				// When a value is found, set it as fullname and be done with it.
    				$members_template->member->fullname = $members_template->member->{$source};
    				break;
    			}
    		}
    		return apply_filters( 'bp_get_member_name_custom', $members_template->member->fullname );
    	}
    	/**
    	 * Filters the display name of current member in the loop.
    	 *
    	 * @since 1.2.0
    	 *
    	 * @param string $fullname Display name for current member.
    	 */		
    }
    
    add_filter( 'bp_get_member_name_custom', 'wp_filter_kses' );
    add_filter( 'bp_get_member_name_custom', 'stripslashes'   );
    add_filter( 'bp_get_member_name_custom', 'strip_tags'     );
    add_filter( 'bp_get_member_name_custom', 'esc_html'       );
    
    // full name to all memebr list
    add_filter('bp_get_member_name','bp_get_member_name_custom');
    	
    	
    // Fullname in group memeber list
    function bp_get_group_member_link_custom() {
    	global $members_template, $bp;
    	
    	$firstname= bp_get_profile_field_data('field=vorname&user_id='.$members_template->member->id);
    	$lastname= bp_get_profile_field_data('field=nachname&user_id='.$members_template->member->id);		
    	if(!empty($firstname) || !empty($firstname)){
    		$fullname = $firstname.' '.$lastname ;
    		return apply_filters( 'bp_get_group_member_link_custom', '<a href="' . bp_core_get_user_domain( $members_template->member->user_id, $members_template->member->user_nicename, $members_template->member->user_login ) . '">' . $fullname . '</a>' );
    	}else{
    		$name_stack = array(
    			'display_name',
    			'user_nicename',
    			'user_login'
    		);
    
    		foreach ( $name_stack as $source ) {
    			if ( !empty( $members_template->member->{$source} ) ) {
    				// When a value is found, set it as fullname and be done with it.
    				$members_template->member->fullname = $members_template->member->{$source};
    				break;
    			}
    		}
    		return apply_filters( 'bp_get_group_member_link_custom', '<a href="' . bp_core_get_user_domain( $members_template->member->user_id, $members_template->member->user_nicename, $members_template->member->user_login ) . '">' . $members_template->member->display_name . '</a>' );
    	}
    	 
    }
    add_filter('bp_get_group_member_link','bp_get_group_member_link_custom');
    
    /**
     * Fetch the display fullname for a user.
     *
     * @param int|string|bool $user_id_or_username User ID or username.
     * @return string|bool The display name for the user in question, or false if
     *                     user not found.
     */
    function bp_core_get_user_displayname_custom( $user_id_or_username ) {
    	if ( empty( $user_id_or_username ) ) {
    		return false;
    	}
    	// return $user_id_or_username;
    	if ( ! is_numeric( $user_id_or_username ) ) {
    		$user_id = bp_core_get_userid( $user_id_or_username );
    	} else {
    		$user_id = $user_id_or_username;
    	}
    	
    	if ( empty( $user_id ) ) {
    		return false;
    	}
    	
    	global $bp;
    	$firstname= bp_get_profile_field_data('field=vorname&user_id='.$user_id);
    	$lastname= bp_get_profile_field_data('field=nachname&user_id='.$user_id);	
    	// return $user_id;
    	if(!empty($firstname) || !empty($firstname)){
    		$fullname = $firstname.' '.$lastname ;
    		return apply_filters( 'bp_core_get_user_displayname_custom', $fullname, $user_id );
    	}else{
    	
    		$display_names = bp_core_get_user_displaynames( array( $user_id ) );
    
    		if ( ! isset( $display_names[ $user_id ] ) ) {
    			$fullname = false;
    		} else {
    			$fullname = $display_names[ $user_id ];
    		}
    		return apply_filters( 'bp_core_get_user_displayname_custom', $fullname, $user_id );
    	}
    	
    }
    add_filter( 'bp_core_get_user_displayname_custom', 'strip_tags', 1 );
    add_filter('bp_core_get_user_displayname_custom');
    add_filter('bp_core_get_user_displayname','bp_core_get_user_displayname_custom');
    
    // Fullname on profile page on other member
    add_filter('bp_displayed_user_fullname','bp_core_get_user_displayname_custom');	
    

    which is creating the following output on my site:

    It was working before updating to WordPress 4.7.

    Can anyone help since i can’t find a solution or error?
    Or am i thinking to complicated?

    i’m currently working on:
    Wordpress Version 4.7.
    Buddypress Version 2.7.4
    BP Xprofile Member Type Field Version 1.0.2

  • You must be logged in to reply to this topic.
Skip to toolbar