skip to Main Content

I want to view this code only with USERNAME, however the public display name taking over. how to only display USERNAME?

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

2

Answers


  1. You are returning $user->display_name;. You need to return $display_name. Try the below code.

    function display_current_user_display_name () {
        $user = wp_get_current_user();
        $display_name = $user->user_login;
        return $display_name;
    }
    add_shortcode('current_user_display_name', 'display_current_user_display_name');
    
    Login or Signup to reply.
  2. Shorter code:

    function display_current_user_display_name () {
        $user = wp_get_current_user();
        return $user->user_login;
    }
    add_shortcode('current_user_display_name', 'display_current_user_display_name');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search