Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to use custom Name fields with xprofile


  • patrix87
    Participant

    @patrix87

    Hi, I’ve found a simple way to change that Name field and the sync back to whatever you want it to be.

    1. Make sure you have buddypress with extended profiles installed.

    2. Create 3 field in the base section of Xprofile
    For demo purpose we will name them :

    First name
    Last name
    Nickname
    *(you can name them differently but make sure you update the function accordingly)

    3. Add that code to the function.php of your child theme.

    
    // modified Xprofile sync.
    function xprofile_sync_wp_profile2( $user_id = 0 ) {
    
    	// Bail if profile syncing is disabled
    	if ( bp_disable_profile_sync() ) {
    		return true;
    	}
    
    	if ( empty( $user_id ) ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	if ( empty( $user_id ) ) {
    		return false;
    	}
        
        // get first name
        $firstname = xprofile_get_field_data('First name',$user_id );
    
        // get last name
        $lastname = xprofile_get_field_data('Last name',$user_id );
        
        // get nickname
        $nickname = xprofile_get_field_data('Nickname',$user_id );
        
        // create fullname
        $fullname = trim( $firstname . ' ' . $lastname );
    
    	bp_update_user_meta( $user_id, 'nickname',   $nickname  );
    	bp_update_user_meta( $user_id, 'first_name', $firstname );
    	bp_update_user_meta( $user_id, 'last_name',  $lastname  );
    
    	global $wpdb;
    
            //you can modify that line to change the displayed name to something else than the nickname
    	$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $nickname, $user_id ) );
    }
    add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile2' );
    add_action( 'bp_core_signup_user',      'xprofile_sync_wp_profile2' );
    add_action( 'bp_core_activated_user',   'xprofile_sync_wp_profile2' );
    
    

    4. This script also change the display name to the nickname but you can modify it to whatever you want. just replace $nickname in the indicated line by $firstname, $lastname or $fullname

    That’s it

Viewing 2 replies - 1 through 2 (of 2 total)

  • Henry Wright
    Moderator

    @henrywright

    Thanks for sharing @patrix87!


    nilje
    Participant

    @nilje

    hi
    thank you very much for this code!

    Just one thing I don’t get to work. The Display Name. So I have first- and last name for ‘Name’, but how will it show this as display name over the whole site?
    You wrote a line to modify it, but it will always show the username, not the ‘Name’ which is consist of first and last name.

    That’s what I tried (I’m a real newbie):
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $firstname, $lastname, $user_id ) );
    Is there something I do wrong?

    And is the third field ‘Nickname’ necessary? Isn’t that the same as ‘Username’?

    Thank you!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use custom Name fields with xprofile’ is closed to new replies.
Skip to toolbar