How To Get WordPress User Display Name

There are times when you want to personalise a user's experience, perhaps in a membership site or in a marketplace style site. In those cases, it's handy to be able to display a users name.

You probably don't want to display the actual usernames, especially when WordPress provides a way for people to customize how they are seen on the site.

To get a WordPress user's display name, you need to get the user object, then use $user->display_name to access the user's name.

That will display whatever the user has set as their display name in this field:

screenshot of WordPress profile admin area with arrow pointing to display name field

If the user hasn't specifically set anything as their display name, it will default to their username. So you don't have to worry about fallbacks.

This is saved in the wp_users table under display_name for the particular user.

How to get the User object

There are a few ways to access the WordPress User object, depending on what information you have.

If you just want to display the current user's name, use wp_get_current_user(). This will only work for the currently logged in user.

$user = wp_get_current_user();

If you have an ID of a specific user who may or may not be logged in, or you're looping through a list of IDs, my preferred way is:

$user = get_userdata( $user_id );

If you have some other information for the user, maybe an email or a username, use:

$user = get_user_by( 'email', 'user@domain.com' );

Change the fields according to what information you have.

Mike Haydon

Thanks for checking out my WordPress and coding tutorials. If you've found these tutorials useful, why not consider supporting my work?

Buy me a coffee

Leave a Comment