10

I'm trying to get the currently login user's id in WordPress. I've used the following code:

function users_list()
{
   global $current_user;
   $current_user =  wp_get_current_user();
   echo 'User ID: ' . $current_user->ID ;
   print_r($current_user);

}

The above code returns the empty result like:

WP_User Object ( [data] => [ID] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [filter] => )
6
  • 2
    What does global $current_user prints?
    – Sisir
    Commented Oct 4, 2014 at 9:46
  • Where is the code you're using to call get_current_user_id()? When are you running this code? Is it before or after the init action?
    – Otto
    Commented Oct 4, 2014 at 22:05
  • It displays WP_User Object ( [data] => [ID] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [filter] => ) @Sisir Commented Oct 6, 2014 at 5:02
  • Your question is confusing. Are you trying to get the current logged in user I'd in the back end. What is the relationship to the second part of your code? Do you need to display the user I'd next to the username in the backend in the "Users" admin screen? Please clarify in an edit Commented Oct 6, 2014 at 5:44
  • Actually I tried to get the currently logged in user id.The second part of the code is for adding the ID column in back-end.Now I'm getting the user id by the above code itself @PieterGoosen. Thanks for spending your golden time with me Commented Oct 6, 2014 at 6:00

4 Answers 4

8

Going by wp_get_current_user() information in the Codex, the function utilizes the global $current_user object and if necessary initializes it before use. As others have stated, get_current_user_id() uses this function on the backend.

Consider /wp-includes/user.php, lines 323-327 (the function definition for this code). At the tail end, the return value is return ( isset( $user->ID ) ? (int) $user->ID : 0 ); — this code will return 0 if the logged-in user is somehow unavailable.

Use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles. See #14024 for details.

This comes from the documentation for wp_get_current_user. If you were using this code within a template, you could be assured that init had already been called. However, if you just randomly grab for that user information before init action is called, you will not get a current user. This explains why you got the User's ID when adding those actions into functions.php (as those actions take place after init), whereas with your original code it's not clear when you invoke that.

Please refer to the Plugin API page for a general idea of the order in which these various actions are invoked.

2
  • 1
    Yes I have checked in wp-includes/user.php file, it has the code code which you have mentioned. Is it be the reason for returning the user id 0? Commented Oct 6, 2014 at 5:12
  • It might also be worth mentioning that if the user is not logged in at all the function also returns 0 (or false)
    – Bysander
    Commented Apr 22, 2015 at 16:34
5

Always use get_current_user_id(); under init action.

Following is the example:

add_action('init', 'myFunction');

function myFunction(){

 $user_ID= get_current_user_id();   

   echo"User number $user_ID is loggedin";
}
1
  • Where under means init action, or any action being called after it, right?
    – jgangso
    Commented Feb 28, 2020 at 8:55
4

As mentioned by the others: If you call the function too early it will return value 0

A good way to check if it's "too early" or not is this kind of checking:

// Do NOT check for action 'set_current_user', but for 'init'!!
if ( ! did_action( 'init' ) ) {
    _doing_it_wrong( __FUNCTION__, 'get_current_user_id() called before the init hook', null );
}
$user_id = get_current_user_id();

The reason why we do not use did_action('set_current_user') is:

If some other code/other plugin called get_current_user_id() too early, it will trigger the hook set_current_user to run. However, the current-user data is not correct at this point, so relying on that action hook is no good idea - only when init is executed we can be sure to have the correct user!

1

For wordpress versions > 3.4:

Use:

$current_user_id= get_current_user_id();

For wordpress versions < 3.4:

Documentation says if the version is less than 3.4 use it with init:

add_action('init', 'get_your_current_user_id');
function get_your_current_user_id(){
        $your_current_user_id= get_current_user_id();
        //do something here with it
}

You can check documention: wp_get_current_user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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