lastlogindate
How to Show User’s Last Login Date in WordPress
November 22nd, 2018 in Wordpress |

Recently, one of our readers asked us how to show a user’s last login date in WordPress. You may need this if you wanted to add an author activity box on your WordPress site. In this article, we will show you how to display the user’s last login date in WordPress.

Method 1: Showing a User’s Last Login Date in WordPress Admin Area

This method is easier, but it will only show a user’s last login date inside “WordPress admin area”.

The first thing you need to do is install and activate the “WP Last Login” plugin.

Upon activation, you need to visit the ‘Users’ page in the admin area. You will notice a new column showing each user’s last login date.

lastloginadmin

At first, it may show ‘never’ for all users. That’s because a user needs to log in since the plugin was activated so that it could capture last login date and store it.

Method 2: Manually Show User’s Last Login Date in WordPress

This method allows you to display a user’s last login date anywhere on your WordPress site.

Simply add this code to your theme’s “functions.php” file or a “site-specific plugin.”

If you’re new to adding code, then please read this guide on pasting the code from the web.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/**
 * Capture user login and add it as timestamp in user meta data
 *
 */
function user_last_login( $user_login, $user ) {
    update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
/**
 * Display last login time
 *
 */
 
function wpb_lastlogin() {
    $last_login = get_the_author_meta('last_login');
    $the_login_date = human_time_diff($last_login);
    return $the_login_date;
}
/**
 * Add Shortcode lastlogin
 *
 */
 
add_shortcode('lastlogin','wpb_lastlogin');
?>

This code adds the last login as a meta key. Each time a user logs in, it saves the time as a meta key value. Before you want to test the plugin, you need to log out of WordPress and then log in again.

You can then display this meta key value using the [lastlogin]shortcode in your WordPress posts and widgets.

If you want to show last login information in your child theme, then you can add this code:

1
<?php echo 'Last seen: '. do_shortcode('[lastlogin]') .' ago'; ?>

lastloginpreview

As you would notice that this code displays relative date and time, i.e. ‘2 hours ago’ instead of full date and time. If you want to display the full date and time, then locate this line in the code above:

1
$the_login_date = human_time_diff($last_login);

Now replace it with this line:

1
$the_login_date = date('M j, Y h:i a', $last_login);

The ‘M j, Y h: I a’ part in this code is called date and time format string. If you want to change how this code displays date and time, then check out our guide on how to change the date and time format in WordPress.

We hope this article helped you learn how to show the user’s last login date in WordPress.