X

Capture users last login date and time

Ninja Forms Review - name form

Adding the first snippet to the functions.php of your wordpress theme will capture a users last login date. Recently I updated the script for the most current version of WordPress. Things have been changed to wpsnipp_get_last_login() and wpsnipp_set_last_login() should be obvious what they do. One will update the last login data in the user meta and the other will let you get the last date so you can display it on your site. We also save the current date and also the previous date so you can choose what you display to admins vs subscribers.



Code Snippet:

// set the last login date
add_action('wp_login','wpsnipp_set_last_login', 0, 2);
function wpsnipp_set_last_login($login, $user) {
$user = get_user_by('login',$login);
$time = current_time( 'timestamp' );
$last_login = get_user_meta( $user->ID, '_last_login', 'true' );

if(!$last_login){
update_usermeta( $user->ID, '_last_login', $time );
}else{
update_usermeta( $user->ID, '_last_login_prev', $last_login );
update_usermeta( $user->ID, '_last_login', $time );
}

}

// get last login date
function wpsnipp_get_last_login($user_id,$prev=null){

$last_login = get_user_meta($user_id);
$time = current_time( 'timestamp' );

if(isset($last_login['_last_login_prev'][0]) && $prev){
$last_login = get_user_meta($user_id, '_last_login_prev', 'true' );
}else if(isset($last_login['_last_login'][0])){
$last_login = get_user_meta($user_id, '_last_login', 'true' );
}else{
update_usermeta( $user_id, '_last_login', $time );
$last_login = $last_login['_last_login'][0];
}

return $last_login;
}

You can then use the following to display the last login date. You may also want to show the previous login date to your users. I save that to user meta in the snippet above. wpsnipp_get_last_login($current_user->ID,true)) adding true will get the previous login date not the current.


Code Snippet:


// show last login date
global $current_user;
get_currentuserinfo();
echo '<p>Previous: Login date: ' . date(“Y-m-d h:m:s'', wpsnipp_get_last_login($current_user->ID,true)) . '</p>';
echo '<p>Current: Login date: ' . date(“Y-m-d h:m:s'', wpsnipp_get_last_login($current_user->ID)) . '</p>';

You can also compare the last login date to the current date and get the days since last login.


Code Snippet:


// how many days since last login
global $current_user;
get_currentuserinfo();

$now = time();
$last_login = wpsnipp_get_last_login($current_user->ID,true);
$datediff = $now – $last_login;

echo '<p>Days since last login: ' . floor($datediff/(60*60*24)) . '</p>';


Some functions in the old version below have been deprecated including get_userdatabylogin(). Please use the above snippet as it is the most current version using get_user_by().


Code Snippet:


function your_last_login($login) {
global $user_ID;
$user = get_userdatabylogin($login);
update_usermeta($user->ID, 'last_login', current_time('mysql'));
}
add_action('wp_login','your_last_login');

function get_last_login($user_id) {
$last_login = get_user_meta($user_id, 'last_login', true);
$date_format = get_option('date_format') . ' ' . get_option('time_format');
$the_last_login = mysql2date($date_format, $last_login, false);
echo $the_last_login;
}


Code Snippet:


<?php
global $userdata;
get_currentuserinfo();
echo 'You last logged in:';
get_last_login($userdata->ID);
?>

Comments  Leave a Reply

  1. in show last login date time format is wrong. please correct it. h:i:s is correct

  2. Great sharing. Really helpful and informative article. Thanks for sharing with us.

  3. Web design Cheshire May 12, 2017 at 8:24 am

    Really handy code here. Thank you.

  4. Toque de Silencio May 5, 2017 at 6:10 am

    Hi,
    I´m new in snippets, so how show I this (or any) snippet in frontend?

  5. Danielconde Uy March 20, 2012 at 1:51 pm

    Deprecated

  6. Hi! Great code, thanks a lot! I have a question though: would it be possible to have a list of all the users showing if they’re connected and the last time they logged in if they’re not?

    I think it would be a very interesting information for any admin working with multiple contributors.

    Thanks again 😉

    1. Thanks glad you like the snippet. This could be done but to that extent I think a plugin would be best suited a few plugins are available.

      http://wordpress.org/extend/plugins/who-is-online/

      1. Looks like a great plugin although a little outdated but I will try it, thanks!

      2. Thanks Kevin,

        Will test that.
        Take care / Lars

  7. Great, I shall test it.

    BUT, do you know where to find the code for being able to see how many times each user login to wordpress and also the last date. (MySQL database to being able to get the lost out?
    Regards / Lars

    1. Well the above will handle the last login date, however the number of times a user has logged in could be done. Ill have to see about adding that as a snippet I looked but could not find anything.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!