Why update_user_meta doesn’t work with display_name in WordPress and how to use pre_user_display_name instead

WordPress is powerful … but occasionally you’ll hit a problem where you hunt for hours online trying to find a solution. You may find plenty of other people searching for the some thing too, but no decent answers.

For me searching for how to use update_user_meta display_name to change the default display name when a new user creates an account was one of those searches.

In case you’re struggling with this too, hopefully this quick post will help you.

 

Firstly the problem:

When a new user registers on the front end of your site, WordPress often decides to make the user name their display name.  Which doesn’t give the user a good experience.  Depending on the site I’m developing, I prefer to have the display name set to Firstname Lastname or just Firstname.

update_user_meta

In WordPress the update_user_meta() function can be used to update a user’s first_name, billing_first_name, nickname and many other fields.  So it would be natural to expect to be able to update their display_name the same way, right?

Well unfortunately display_name can’t be updated with update_user_meta.

Use pre_user_display_name instead

The pre_user_display_name hook filters a user’s display name before the user is created or updated. That’s what we need to use.

Here’s example code I used when I had added a “First Name” field to the registration form.  I wanted the display name to be set as their first name.

Note – This was on a WooCommerce site, and the name of the first field on the registration form was “billing_first_name”.  This can work on any WordPress site – you just need to find what the name of the field is on the form.

This was the html on the registration form – you can see that “billing_first_name” was the name of the form field on the registration form where they inserted their first name.


<input class="input-text" name="billing_first_name" type="text" value="" />

This code was added into my functions.php file in my child theme, which sets the display name to be the first name.

add_filter('pre_user_display_name','default_display_name');

function default_display_name($name) {

if ( isset( $_POST['billing_first_name'] ) ) {
$name = sanitize_text_field( $_POST['billing_first_name'] );
}
return $name;
}

 

Or alternatively, if you want the display name to be “firstname lastname” use this code:


add_filter('pre_user_display_name','default_display_name');
function default_display_name($name) {
if ( isset( $_POST['billing_first_name'] ) ) {
$firstname = sanitize_text_field( $_POST['billing_first_name'] );
}
if ( isset( $_POST['billing_last_name'] ) ) {
$lastname = sanitize_text_field( $_POST['billing_last_name'] );
}
$name = $firstname . ' ' . $lastname;
return $name;
}

 

Has this article been helpful?  Please leave a comment below.

If you would like to know how to add extra fields to your WooCommerce registration page here is a great article >

Josh Moore

Josh Moore

I love marketing and technology. I run Duoplus Online Marketing and enjoy helping companies grow their income especially through using online marketing effectively.
Previous post Clearing All Filters in WooCommerce Layered Navigation
Next post Changing the default Demo Store Notice in Woocommerce