0

I use this function to get the last time a user logged in.

function jkn_last_login($user_login) {
    $user = get_user_by( 'login', $user_login );
    global $last_login;
    $last_login = array();
    $last_login[$user->ID] = get_user_meta($user->ID,'last-login',true);
    update_user_meta( $user->ID, 'last-login', time() );
}
add_action('wp_login','jkn_last_login',12,3);

As you can see i define a global $last_login to save the last login time.

But if i try to echo the value of $last_login nothing happen.

global $last_login;
global $current_user;
echo $last_login[$current_user->ID];
0

Don't use global variables. You can do it like this without global variable:

global $current_user;
echo get_user_meta( $current_user->ID, 'last-login', true );
0

Not sure if you can use the global keyword twice?

Try...

global $current_user, $last_login; echo $last_login[$current_user->ID];

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.