• Resolved belal_hafez

    (@belal_hafez)


    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)
  • Does global $user_ID; var_dump($user_ID); give you anything?

    Thread Starter belal_hafez

    (@belal_hafez)

    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!?

    function less_generic_than_Insert() {
        global $user;
        var_dump(get_defined_vars());
        die;
    }

    See if that works.

    Thread Starter belal_hafez

    (@belal_hafez)

    Should this function display the variables along with their variables?
    I placed your function at the bottom, but I didn’t get anything different.

    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

    (@belal_hafez)

    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

    (@belal_hafez)

    I think this would be helpful but I can’t understand the solution!!

    Thread Starter belal_hafez

    (@belal_hafez)

    I found the solution here ??

    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.