1

I'm trying to force a specific "display_name" using data collected in a wpforms registration form.

In the registration form, I've got a field for "last name in high-school" and a field for "nickname in high-school" -- and set to save as user_meta values high_school_last_name and hs_nickname respectively.

So, I'm trying to create an action on registration that will set display_name to

first_name "hs_nickname" (high_school_last_name) last_name

(but also accounting for if someone doesn't fill out those fields).

But I think I'm having a problem because the code I'm using looks for values in users_meta that aren't there yet.

Here's my code:

add_action( 'user_register', 'customize_display_name' );

function customize_display_name( $user_id )
{
    $data = get_userdata( $user_id );
    $maiden = $data->high_school_last_name;
    $nick = $data->hs_nickname; 
    $first = $data->first_name;
    $last = $data->last_name;
    $maiden_name = !empty($maiden) ? ' ('.$maiden.') ' : '';
    $nick_name = !empty($nick) ? ' "'.$nick.'" ' : '';
    $firstname = ' '.$first.' ';
    $lastname = ' '.$last.' ';

    wp_update_user( 
        array (
            'ID' => $user_id, 
            'display_name' => $firstname.$nick_name.$maiden_name.$lastname
            ) 
        );
}

I've also tried a version where I use get_user_meta instead of get_userdata... same issue...

What's happening is the display_name is being set as just first_name last_name (i.e., as though the other fields are empty).

Is there a way to achieve what I want? Do I need to hook into something in wpforms instead of user_register?

Thanks, Scott

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.