How to Show User’s Last Login Date 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 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.

First thing you need to do is install and activate the WP Last Login plugin. For more details, see our step by step guide on how to install a WordPress plugin.

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

Last login date column in WordPress admin area

At first it may show ‘never’ for all users. That’s because a user needs to login 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 code from the web.

 <?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 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 logout of WordPress and then login again.

You can then display this meta key value using [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:

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

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:

$the_login_date = human_time_diff($last_login);

Now replace it with this line:

$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 date and time format in WordPress.

We hope this article helped you learn how to show user’s last login date in WordPress. You may also want to see our guide on how to add author info box in WordPress posts.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

American Fido, LLC is not responsible for the opinions and commentary presented in the publishing of these articles.
(Visited 1 times, 1 visits today)