Capture users last login date and time

Capture users last login date and time

Online by JSC0d3
January 19, 2016 | | 117 recognitions

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 wp_get_last_login() and wp_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.

// set the last login date
add_action('wp_login','wp_set_last_login', 0, 2);
function wp_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 wp_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. wp_get_last_login($current_user->ID,true)) adding true will get the previous login date not the current.

 // show last login date
  global $current_user;
  get_currentuserinfo();
  echo date("Y-m-d h:m:s", wp_get_last_login($current_user->ID,true));
  echo date("Y-m-d h:m:s", wp_get_last_login($current_user->ID));

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

 // how many days since last login
  global $current_user;
  get_currentuserinfo();
  $now = time();
  $last_login = wp_get_last_login($current_user->ID,true);
  $datediff = $now - $last_login;
  echo floor($datediff/(60*60*24));

 

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post

Leave here an impression