Capture users last login date and time

Adding the first snippet to the functions.php of your wordpress theme will capture a users last login time. Add the second snippet to the location you wish to display the users last login time.



function your_last_login($login) {
global $user_ID;
$user = get_userdatabylogin($login);
update_usermeta($user->ID, 'last_login', current_time('mysql'));
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;


global $userdata;
echo  'You last logged in:';

Leave a comment