Editing the user's display name

I have a form that allows people to update their display_name. However, this doesn’t work. So what I’m doing is the following:

  1. I prefill the form field with the {wp_user_display_name}
  2. Then I use Brickforge’s “update user meta” to update the nickname meta key. This does update it in the backend!
  3. Now I want the nickname to synchronize automatically to the “Display name publicly as” field, but this is where the trouble starts as this field is not auto updating whenever I save the form.

Any ideas on how to fix this or sync the 2 fields? Huge thanks in advance as I’ve been struggling with this for over 5 hours now.

Just deactivate the default form reset on the “Others” tab and you should be fine :slight_smile:

That also didn’t work, I have now figured it out, needed to add this to the functions.php:

$user_id = get_current_user_id();  // get current user ID
$nickname = get_user_meta($user_id, 'nickname', true);  // get nickname

if ($user_id && $nickname) {
    wp_update_user(array('ID' => $user_id, 'display_name' => $nickname));
}

Hmm…this updates the user on every page load. I think I do not understand the problem correctly :slight_smile:

Ohh, that is also not really the solution then… this was given me by Chat GPT 4.

Okay, let me rephrase it with image:
image
I want to update the red marked text here, which is: {wp_user_display_name}

I do this with the following form:
Fields:



(i tried display_name as a meta key but that didnt work either).

This only updates the nickname on the backend, but no matter what I did this section (which controls which name is shown on the site) doesn’t update:
image

Only the field above it updates:
image

Hopefully this shines a bit more light on the issue.

Okay, understood.

To make it more clean, wrap your code into a function and create an additional custom action to update the user after updating the user meta.

Or, at least, check if the nickname is already the display name…and update the user in the database only if not:

$user_id = get_current_user_id();  // get current user ID
$nickname = get_user_meta($user_id, 'nickname', true);  // get nickname

if ($user_id && $nickname) {
    $user = get_userdata($user_id);
    $current_display_name = $user->display_name;

    if ($current_display_name !== $nickname) {
        wp_update_user(array('ID' => $user_id, 'display_name' => $nickname));
    }
}
1 Like

Ohh this is perfect thank you!
The other code also logged users out after a few reloads for some reason. Would that be the case with this code as well? Or since this is a function it wouldnt have that problem?