Title: $current_user doesn&#039;t work inside login hook
Last modified: August 20, 2016

---

# $current_user doesn't work inside login hook

 *  Resolved [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/)
 * I’m using $current_user to get user’s ID & it works fine but when I use this 
   variable inside login hook, it doesn’t work. Here is my code:
 *     ```
       add_action('wp_login','Insert');
   
       function Insert(){
              global $wpdb;
       	$table_name = $wpdb->prefix."userlog";
       	$date = date('Y-m-d H:i:s');
       	$wpdb->insert( $table_name, array('user_id' => $current_user->ID,'log_date_time' => $date ));
       }
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078418)
 * Does `global $user_ID; var_dump($user_ID);` give you anything?
 *  Thread Starter [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078425)
 * Still returns 0. I don’t know why this variable doesn’t work inside the hook 
   function while it works outside the function. Whats the conflict between the 
   login hook & $current_user!?
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078480)
 *     ```
       function less_generic_than_Insert() {
           global $user;
           var_dump(get_defined_vars());
           die;
       }
       ```
   
 * See if that works.
 *  Thread Starter [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078496)
 * Should this function display the variables along with their variables?
    I placed
   your function at the bottom, but I didn’t get anything different.
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078497)
 * It is only a test case function. It should dump a sizable array and die. I don’t
   know what you mean by ‘the variables along with their variables’. It should be
   an array containing the $user object created when a user logs in. It works for
   me. I have tested it several times, logging out and back in.
 * Sorry… you have to hook it to wp_login as in your function above. I guess that
   wasn’t clear.
 *  Thread Starter [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078499)
 * Sorry I meant variables along with their values. Ok, now I’m getting the entire
   $user array & it returns correct values.
 *  Thread Starter [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078500)
 * I think [this ](http://wordpress.stackexchange.com/questions/59683/get-user-logged-in-status-from-within-a-plugin-current-user-not-defined)
   would be helpful but I can’t understand the solution!!
 *  Thread Starter [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * (@belal_hafez)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078504)
 * I found the solution [here](http://www.webtechideas.com/how-to-get-last-login-time-in-wordpress/)
   🙂
 * Thanks to Google & WordPress developers.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘$current_user doesn't work inside login hook’ is closed to new replies.

## Tags

 * [current_user](https://wordpress.org/support/topic-tag/current_user/)

 * 8 replies
 * 2 participants
 * Last reply from: [belal_hafez](https://wordpress.org/support/users/belal_hafez/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/current_user-doesnt-work-inside-login-hook/#post-3078504)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
