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.

About Josh Moore

I run an online marketing agency and we build sites in Wordpress. While the developers on my team do the real development work, I enjoy solving problems and documenting things I discover which will hopefully help others.