How to Show the Logged in Username in the WordPress Sidebar

Posted on Updated on

A lot of WordPress websites don’t care much about membership. Conventional blogs and even business pages focus more on transmitting information rather than accumulating deep interactions with users. However, there are a large number of commerce sites for example that use WordPress as a platform. These pages rely heavily on site membership for various reasons such as being able to store user information, preferences, payment methods etc. Have you ever visited one of these sites only to wonder whether or not you’re logged in? Not all of them will be able to personalize their content for you so it’s not always easy.

One way to give a clear and immediate visual indicator of whether or not a visitor is logged in, is to display a short message on the lines of “Welcome xyz”  or “Logged in as: xyz”. This provides an instant cue about whether or not someone has already signed in. And if not, you can easily display a “Login” link instead. In this tutorial, I’ll show you how to create such a message in your WordPress sidebar.

Getting the Display Name

The first step is deciding on which name you want to call your user. The profile section of WordPress contains a number of alternatives. You can use the “nickname”, the first name, last name, or the chosen “Display name”. For this example, I’m going to use the last variable. It’s presented as a drop-down box in the WordPress profile settings as shown here:

This way you present the least chances of going wrong. We want to display our custom message in the WordPress sidebar so we’re going to make use of a short code to achieve this effect. Open your functions.php file and paste the following before the closing ?> PHP tag:

function show_loggedin_function( $atts ) {

	global $current_user, $user_login;
	add_filter('widget_text', 'do_shortcode');
	if ($user_login) 
		return 'Welcome ' . $current_user->display_name . '!';
		return '<a href="' . wp_login_url() . ' ">Login</a>';
add_shortcode( 'show_loggedin_as', 'show_loggedin_function' );

This creates a shortcode by the name of “show_loggedin_as”. If you don’t know how to insert custom PHP code into WordPress, you can take a look at my earlier tutorial on how to do this.

Normally, shortcodes don’t work in text widgets. But we’ve included the line “add_filter(‘widget_text’, ‘do_shortcode’);” which allows the magic to happen. So open up a text widget in the “Appearance->Widgets” section of the WordPress dashboard and insert a regular text widget using the shortcode “_loggedin_as” that we just created as shown here:

use shortcode in text widget

Now when you visit your website as a logged in user, you should see a welcome message on the lines of this:

welcome message

Conversely, anyone who hasn’t logged in yet will see the login link:

login link

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

These allow you a greater choice and you can make use of the email ID as well as the first name and last name for better personalization.

The login link and the welcome message are a great visual cue for users to quickly let them know whether or not they need to sign in to your website. If subscriptions and WordPress memberships are a large part of your site operations, you should definitely implement some form of the solution above if your theme doesn’t already do it for you.

13 Comments on “How to Show the Logged in Username in the WordPress Sidebar”!

  • Hello, thanks for taking out time to write this post. I’ve tried running it on my site like you instructed, but the the shortcode appears instead of the user or login link.

  • Good !

  • Thanks for sharing, I found that this is working for me in widgets and posts. I also wanted to show the first and last name so I modified the code like this:
    return ‘Welcome ‘ . $current_user->user_firstname . ‘ ‘ . $current_user->user_lastname . ‘!’;
    Hope this helps someone :)

  • Hello, it worked fine on my site, when debugging enabled it shows an error in which ‘get_currentuserinfo’ has become obsolete since version 4.5.0 and that is for me to upgrade to ‘wp_get_current_user ()’ but I am still new to php and I do not know how to change this would have help me?

    • Hi Luizz,

      In the code given in the article, you can try replacing get_currentuserinfo() with wp_get_current_user() in the 3rd line of code.

      Admittedly I haven’t tested this yet, but try it out and tell me if it works!

      • A substituição do get_currentuserinfo () por wp_get_current_user () funcionou perfeitamente muito obrigado pela atenção !

  • Hi,

    I have QUESTION:
    how do i change the WP login link with Woocommerce MYACCOUNT link here:
    ‘<a href="' . wp_login_url() . '

    Pleased to hear – thanx in adnvance

  • Hi what if I want it to show Logout insted of welcome?

  • Thanks for sharing, I found that this is working for my woocommerce my account page.

  • Works awesomely, thank you for doing this! Our members will appreciate it.

  • Hi there thanks for the code. but i wanted to know how to redirect the login to the current page instead. please thank you

  • Do i paste this directly into php file? or in theme options its not working for me

  • Is there a way to add the avatar that they have too?

Leave a Reply

Your email address will not be published. Required fields are marked *