If your WordPress site allows users to register and login, there are times where you might need the last login time of your users for security troubleshooting, or simply to display them in the frontend.
While there are plenty of plugins that allow you to perform the above task, in this article, we will show you how you can do so without plugins and by adding a few functions to your
Capturing User Last Login Time
The easiest way to capture a user last login time is to hook onto the
functions.php file in the theme folder and add the following code:
This will capture the time when a user login.
If you want to save the last 10 login time of the user, use this code instead:
The above code will save the last 10 login time as an array to the user meta “last_login”.
Displaying User last login time in the backend
To add a “last_login” column to the Users section in WordPress, add the following code to your
Displaying user last login time in the frontend
To display user last login time in your theme, simply call the function
get_user_last_login($user_id). To return the value instead of printing it out to the screen, use the function
Image credit: Login