You’re managing a WordPress website with multiple users. It might come handy one day to see when the last time a user logged in. Maybe all of a sudden some pages happen to be deleted (by accident) by a member of your team, and you want to blame someone. Who logged in, just moments before disaster struck?
Or it can be useful for monitoring which users are still logging in from time to time to contribute to the website.

This post explains how you can write a little plugin yourself that keeps track of the last time a user logged in, and shows you via the users list.

1. Create the plugin

Inside your theme’s /wp-content/plugins folder, add a new folder called my-last-login. Inside that folder, create a new file called my-last-login.php. This file will hold all the logic. Add the following code at top of the file. You can swap my name and website with yours, or remove both lines of code.

<?php

/**
* Plugin Name: Last Login
* Description: Shows the time a user was last logged in.
* Version: 0.1
* Author: Turpoint
* Author Uri: https://turpoint.com
*/

2. Activate the plugin

Go ahead and click on “Plugins” in the admin menu. You should see your newly created plugin. Go ahead and activate it.

3. Storing the time when a user logs in

To store the time into the database, we need to call a function at the moment a user logs in. We can do this by using the wp_login hook. Add this code to last-login.php.

/**
 * Store the time when a user logs in
 */
add_action('wp_login', function($login, $user) {
    // Store the time as user meta
    update_user_meta($user->ID, 'last_login', time());
}, 10, 2);

4. Create a column

When we list all the users via the admin dashboard, we want to add a new column to the overview that will display the timestamp.

/**
 * Add a column to the users list
 */
add_filter('manage_users_columns', function($columns) {
    foreach($columns as $key => $column) {
        if ($key == 'role') {
            $columns['last_login'] = __('Last login');
        }
    }
    return $columns;
});

At this point, you should see that a new column was appended to the table. However, it’s still empty. Let’s change that in the next step.

5. Print the timestamp

The last step is using the manage_users_custom_column filter to print the timestamp per user row.

/**
 * Shows the time in the column / user
 */
add_filter('manage_users_custom_column', function($value, $column_name, $user_id) {
    // Bail if this isn't the 'last_login' column
    if ($column_name != 'last_login') {
        return $value;
    }

    // Retrieve the time by user id
    $last_login = get_user_meta($user_id, 'last_login', true);

    // Print '-' if the user hasn't logged in yet
    if (!$last_login) {
        return '-';
    }

    // Return the timestamp in the datetime format as set in your WordPress settings
    return date(get_option('date_format') . ' ' . get_option('time_format'), (int)$last_login);
}, 10, 3);

And that’s it! Be sure to get in touch if you have any questions.