Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Get Logged-in User’s Info in WordPress for Personalized Results

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Recently we showed you how to create a personalized experience for your users by allowing them save their favorite posts in a personalized library. You can take personalized results to another level by using their first name at places (i.e the welcome screen). Luckily, WordPress makes it really easy to get the logged-in user’s information. In this article we will show you how to retrieve information pertaining to the currently logged in user.

We will utilize get_currentuserinfo(); function. This could be used anywhere in your theme (header, footer, sidebar, page-template etc). In order for this to work the user must be logged-in. So we will need to use the conditional statement is_user_logged_in(). Example code:

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->
<?php } else {   ?>
    <!-- here is a paragraph that is shown to anyone not logged in -->

<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>

Now for the logged_in users, we can show a custom message for instance, “Hey Syed, Everything is here, right where you hoped it would be”. The above code will turn into something like this:

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->

<?php global $current_user; get_currentuserinfo(); ?>

<h1>Hi <?php echo $current_user->user_firstname; ?></h1>

<p>Everything is here, right where you hoped it would be :)</p>

<?php } else {   ?>
    <!-- here is a paragraph that is shown to anyone not logged in -->

<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>

The magic code that we added above is $current_user->user_firstname; which is working because the call to get_currentuserinfo() places the current user’s info into $current_user. You can use the similar method to get other information about the user such as their login, user ID, email, website etc.

Here is a sample usage of all information:

<?php global $current_user;
      get_currentuserinfo();

      echo 'Username: ' . $current_user->user_login . "
";
      echo 'User email: ' . $current_user->user_email . "
";
      echo 'User first name: ' . $current_user->user_firstname . "
";
      echo 'User last name: ' . $current_user->user_lastname . "
";
      echo 'User display name: ' . $current_user->display_name . "
";
      echo 'User ID: ' . $current_user->ID . "
";
?>

Hope this helps. Combining this with the ability to add favorite posts, you can easily create a personalized experience.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

10 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. S Meredith says

    Hi Syed,
    Fantastic site.
    It’s helped me a lot with understanding WP and editing my own site.
    My question is, once I’ve inserted the above function into my child themes functions.php, how would i create a shortcode so that I can then display this user information on a generic page?

    Are shortcodes the best way to allow this to be displayed on any page i’d like.

    For example, I have a static page that I would like to display ‘Welcome {user_name}’ and their profile picture.

  3. Pavan says

    I know nothing about php. I just want to show the username of my customer on TOP bar navgation, and from there he can access his account. How do I do that.

  4. Faruk says

    What if the user not logged in. Can the wordpress still send personalised email with his name? and if sa what is the variable and how?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WPBeginner Assistant
How can I help you?

By chatting, you consent to this chat being stored according to our privacy policy and your email will be added to receive weekly WordPress tutorials from WPBeginner.