Writing a plugin that tells you the last time a user was logged in to WordPress

How you can write a little plugin yourself that keeps track of the last time a user logged in.

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.