Family Encyclopedia >> Electronics

How to Retrieve Logged-In User Info in WordPress for Personalized Experiences

We've recently shared how to let users save favorite posts to a personal library. Elevate personalization by incorporating their first name into your site—such as on a splash screen. WordPress simplifies accessing logged-in user data. This guide, drawn from years of hands-on theme development, walks you through retrieving current user information securely and effectively.

Use the get_currentuserinfo() function anywhere in your theme (header, footer, sidebar, or templates). Always wrap it in the is_user_logged_in() conditional to ensure the user is authenticated. Here's the foundational code:

if ( is_user_logged_in() ) {
    get_currentuserinfo();
    echo 'Hi ' . $current_user->user_firstname . '!';
}

For registered users, craft custom messages like "Hi [First Name], everything is here, right where you expect it to be." Example:

<h1>Hi <?php echo $current_user->user_firstname; ?>!</h1>
<p>Everything is here, just where you expect it to be :)</p>
<p>By registering, you can save your favorite posts for future reference.</p>

The key addition is $current_user->user_firstname, populated by get_currentuserinfo(). Access other details like login name, ID, email, website, and more using similar properties.

Complete example displaying all key user data:

if ( is_user_logged_in() ) {
    get_currentuserinfo();
    echo 'User Login: ' . $current_user->user_login . "\n";
    echo 'User Email: ' . $current_user->user_email . "\n";
    echo 'First Name: ' . $current_user->user_firstname . "\n";
    echo 'Last Name: ' . $current_user->user_lastname . "\n";
    echo 'Display Name: ' . $current_user->display_name . "\n";
    echo 'User ID: ' . $current_user->ID . "\n";
}

Note: While get_currentuserinfo() works in many setups, modern WordPress recommends wp_get_current_user() for future-proofing. Pair this with favorite post features to deliver truly engaging, user-centric sites we've built for clients.