Skip to:
Content
Pages
Categories
Search
Top
Bottom

Update Display Name Using XProfile Data


  • josh.grisdale
    Participant

    @joshgrisdale

    Hello,

    I know that I can change a User’s Display Name via WordPress Hooks.

    ie this changes a user’s display name to be their user id on registration:

    function update_names( $user_id ) {
    	$data = get_userdata( $user_id );
    	$username = $data->user_login;
    	wp_update_user( 
    		array (
    			'ID' => $user_id, 
    			'display_name' => $username,
    		) 
    	);
    }
    add_action( 'user_register', 'update_names' );

    I have a XProfile field called “Display Name” (field_id is “1”). What I want to do is:

    When a User goes to their Profile Edit page and changes the field, when they click “Save Changes” the system will change ‘display_name’ to the text they entered.

    How do I edit the code that fires on profile update?

    Thanks

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

  • josh.grisdale
    Participant

    @joshgrisdale

    Never mind, got it!

    For others who may be interested in the future:

    function displayname_sync( $user_id ) {
    	$newname = xprofile_get_field_data( 'Display Name', $user_id );
    	if ($newname !== '') {
    		wp_update_user( array( 'ID' => $user_id, 'display_name' => $newname ) );
    	}
    }
    add_action( 'xprofile_updated_profile', 'displayname_sync' );
    

    MorgunovVit
    Participant

    @morgunovvit

    Hi, Josh!
    I am trying to solve a similar problem, but only I need to make the display_name = First_Name + Last_Name fields of the BuddyPress xprofile.

    I have been tormenting for a long time and can’t find a suitable hook that firing when the xprofile fields First_Name and Last_Name have already been written in the database.


    josh.grisdale
    Participant

    @joshgrisdale

    Hi,

    I’m not really a programmer and just hacked together some stuff I found…

    but,

    First of all I assume that xprofile_get_field_data only gets feilds in the Extended Profile tab of the user, so you would either need to add those fields to the Extended Profile, or you would need another wordpress function to get it from the regular profile likely

    $current_user->user_firstname (https://codex.wordpress.org/Function_Reference/wp_get_current_user)

    $newname = $current_user->user_firstname . $current_user->user_lastname;

    I would guess…


    MorgunovVit
    Participant

    @morgunovvit

    Hi!
    I’ve solved it! If somebody interesting of it, there’s the solutions here: https://buddypress.org/support/topic/how-to-set-display_name-xprofile-custom_field-at-the-user-registration/

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar