1

I was trying this code to get current user info, but showing nothing. My WordPress version is 3.3.1

<?php
    wp_get_current_user();
    /**
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( !($current_user instanceof WP_User) )
     *     return;
     */
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';
?> 

The output is:

Username:

User email:

User first name:

User last name:

User display name:

User ID:

7

Have you tried to go with the "Safe usage" alternative given in the commented section?

I honestly don't have any experience with wp_get_current_user(), since I never use it, but anyhow, this ought to work:

global $current_user;

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

wp_get_current_user() should do the same, as it is nothing but a wrapper for the first two lines above, nonetheless, the above has got to work.

1

Prior to Wordpress 4.5 get_currentuserinfo() was an acceptable option, but is now deprecated, in favor of wp_get_current_user().

You can set the wp_get_current_user() as a variable to easily access it in your function or page:

$current_user = wp_get_current_user();

Example:

if ( is_user_logged_in() ) {

    $current_user = wp_get_current_user();

    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: '. $current_user->ID . '<br />';
    echo 'User avatar: ' . get_avatar( $current_user->ID, 64 ) . '<br />';

} else {

    echo 'User not logged in.';

}

Also, it's important to make sure this is all being done after init to avoid blank output.

1

This is how you can get current user:

global $current_user;
$current_user = wp_get_current_user();

After that, you can use $current_user->ID where you want.

Example:

if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    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 . '--';
} else {
    echo 'User not logged in.';
}
  • Why do you need/use the global? – Mark Kaplun Jan 27 '16 at 4:32
  • Mark I just updated the answer, but to answer your question, the global is unnecessary as that is the main reason to use wp_get_current_user() (click the link to see where the function accesses the global if you're curious as to how it works). – Bryan Willis Jan 27 '16 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.