Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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.

Showing a user's last login date in WordPress

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions, then continue reading.

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'; ?>

Showing user's last login information in author bio box

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

7 CommentsLeave a Reply

  1. Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. It is worth being aware of that Wordfence stores the last login date as a meta field.

  3. Thanks for the script.
    I have an observation. All users sees the last login of the admin (author).
    Is there a way for each user to see their own last login?

    Please help. Thanks

    • For showing individual last logins we would recommend using the plugin method as a manual code method would be more advanced than what we would recommend for beginners.

      Admin

  4. Hello,

    It works great but the time stamp when the line is changed for date/time still shows a 7 hour time difference?

    How can I make the last login time show Pacific Standard Time. That is my timezone.

    Please advise, thanks.

    Cheers!

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.