Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Show User’s Last Login Date in WordPress (2 Easy Methods)

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Recently, one of our readers asked us how to show a user’s last login date in WordPress.

You may need this if you want to add an author activity box on your WordPress site. Or, if you run a multi-author site, then you can check when each user logs in.

In this article, we will show you how to show a user’s last login date in WordPress.

How to show a users last login date in WordPress

Why Display User’s Last Login Date in WordPress?

Adding a user’s last login column to your WordPress dashboard and front end helps keep track of when authors, admins, editors, contributors, and other users are logging in and out of your WordPress site.

This is extremely useful for securing your website from any suspicious activity and cyber threats.

For instance, let’s say your site is exposed to a vulnerability caused by a WordPress plugin installed by a user. You can use the last login date to track users and then quickly fix the problem.

Similarly, showing the user’s last login date makes it easier to monitor the time admins, contributors, authors, and others are spending working on your site. You can use this on a multi-author site and track the time spent by each user.

That said, there are 2 ways to show the user’s last login date in WordPress. First, you can use a plugin to show the login date inside the WordPress admin panel. Second, you can manually show the last login date using a custom code snippet.

Simply click the links below to jump ahead to your preferred section:

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

You can easily show the last login date using the WP Last Login plugin. This method is easier, but it will only show a user’s last login date inside the WordPress admin area.

The 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.

The best thing is that the plugin works out of the box, and there are no additional settings you have to worry about.

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

View last login in dashboard

At first, it may show ‘never’ for all users. That’s because a user needs to log in since the plugin has been activated so that it can capture the 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.

However, it requires adding custom code to your theme’s functions.php file or a site-specific plugin. We don’t recommend editing the functions.php file directly because the slightest mistake can break your website.

An easier way of adding custom code to your site is by using the WPCode plugin. It is the best code snippet plugin for WordPress that helps manage, organize, and insert custom code anywhere on your site without breaking anything.

First, you’ll need to install and activate the WPCode plugin. If you need help, then please see our guide on how to install a WordPress plugin.

Note: There is also a free WPCode plugin that you can use for this tutorial. However, upgrading to the premium plugin will give you access to a cloud-based code snippet library, smart conditional logic, and more.

Upon activation, you can go to Code Snippets » + Add Snippet from the WordPress dashboard.

Next, simply hover over the ‘Add Your Custom Code (New Snippet)’ option and click on ‘Use snippet’.

Add your custom code

After that, you can copy the following code and add it into the Code Preview area:

<?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.

After entering the code, you will also need to enter a title for your custom code and select the ‘Code Type’ as PHP Snippet.

Add last login code to WPCode

From here, you can scroll down and choose where to insert the code.

For this tutorial, we will use the default method and keep it to the ‘Auto Insert’ method. This way, the custom code will run everywhere on your website.

Insertion method in WPCode

Once you are done, simply scroll back to the top.

Go ahead and activate your custom code snippet and click the ‘Save Snippet’ button.

Activate and save snippet in WPCode

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

You can then display the last login date value using the[lastlogin] shortcode in your WordPress posts and widgets.

Simply edit a post or page and go to the content editor.

How to add a shortcode block to WordPress

From here, add a Shortcode block where you want to show the last login date and paste the shortcode.

Similarly, if you want to show the last login information in your child theme, then you can add this code instead:

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

Before you test the plugin, you will need to log out of WordPress and then log in again.

Then, you should visit your WordPress website to see the login date in action.

User last login time

As you will notice, this code displays the relative date and time, ‘2 hours ago’ instead of the full date and time.

If you want to display the full date and time, then you need to find 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 the date and time format string. If you want to change how this code displays the date and time, then just 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 a user’s last login date in WordPress. You may also want to see our guide on how to add an author info box in WordPress posts and the ultimate guide to WordPress security.

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.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

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. Syed Balkhi says

    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. Babak Fakhamzadeh says

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

  3. Fredrick Arije says

    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

    • WPBeginner Support says

      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. Bobby says

    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.