4

I have seen similar question Here at First and Second. But these two are not close to my code.

Let me show my code here.

add_action('wp_login', 'set_last_login');

//function for setting the last login
function set_last_login($login) {
    $user = get_userdatabylogin($login);
    $curent_login_time = get_user_meta( $user->ID , 'current_login', true);
    //add or update the last login value for logged in user 
        update_usermeta( $user->ID, 'last_login', current_time('mysql') );
    }
}

and I wrote a get function to get the login access time. But its receiving the current login time due to the update after login. Is there any way to change it. I need to show the last login time.

How can I extend this function? . I am expecting a suggestion or code help to do this one.

5

Finally I found answer by myself. Let's explore it with two user meta's current_login, last_login.Lets see the code.

//function for setting the last login
function set_last_login($login) {
    $user = get_userdatabylogin($login);
    $curent_login_time = get_user_meta( $user->ID , 'current_login', true);
    //add or update the last login value for logged in user
    if(!empty($curent_login_time)){
        update_usermeta( $user->ID, 'last_login', $curent_login_time );
        update_usermeta( $user->ID, 'current_login', current_time('mysql') );
    }else {
        update_usermeta( $user->ID, 'current_login', current_time('mysql') );
        update_usermeta( $user->ID, 'last_login', current_time('mysql') );
    }
}

This will helps you to save the current and last login times. you can write an get last time function to get last login time. Here I wrote an article regarding the Last login and its working Kvcodes- How to set User Last Login Date and Time in WordPress Without Plugin

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.