[show_loggedin_as]
Of course, you can customize the solution to suit your own needs as necessary. In addition, you can utilize a large number of other variables and not just the display name. In the code above, we are using "$current_user->display_name", but you can also replace it with any of the following:
$current_user->user_login
$current_user->user_email
$current_user->user_level
$current_user->user_firstname
$current_user->user_lastname
$current_user->ID
Source: https://www.webhostinghero.com/blog/show-username-in-wordpress-sidebar/
function show_loggedin_function( $atts ) {
global $current_user, $user_login;
get_currentuserinfo();
add_filter('widget_text', 'do_shortcode');
if ($user_login)
return "$current_user->user_firstname";
else
return '<a href="' . wp_login_url() . ' ">Login</a>';
}
add_shortcode( 'show_loggedin_as', 'show_loggedin_function' );