WordPress get display name

Depending on whether you have the user’s ID (If not, checkout: Get user ID in every situation) you can get the display name by creating the user object first:

// Different ways getting the user object
$user = new WP_User( $user_id );

// Current user object
$user = wp_get_current_user();

// Get user by ID, email, slug or login
$user = get_user_by( 'ID', $user_id );

// Get user data by user id
$user = get_userdata( $user_id );

When the user object is created you can simply obtain the display name from it as follows:

// Get display name from user object
$display_name = $user->display_name;

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.


    No comments yet..

Leave a reply

Feel free to ask questions or make comments.