Skip to content
Advertisement

How can we display current user display name with shortcode?

I got this shortcode function to work to show current user display name, but now for some reason it is displaying the user’s first name. Is there another way to get current user’s display name and output it with shortcode?

// [current_user_display_name]
function display_current_user_display_name () {
    $user = wp_get_current_user();
    $display_name = $user->display_name;
    return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

Advertisement

Answer

If you go to a user’s profile (User’s > All users > search for the user you want) then one of the settings is called “Display name publicly as”. As you can see from the different options available, the user is able to set their display name to be their first name. If you want the shortcode to display the username use $user->user_login. If you want the first and last name you can get that info from get_user_meta( $user->ID ).

Be careful when using the first and last name as not all users will have a first and last name saved. When there is no first or last name I usually fallback to using the username.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement