Want to show when a user last logged in to your WordPress site? Monitoring user activity can be important for security and engagement reasons.
In this comprehensive guide as a WordPress professional, I‘ll compare methods to easily show a user‘s last login date anywhere in WordPress.
Why Show User Last Login Dates
Here are 3 key reasons for displaying user last login information in WordPress:
Site Security – Monitoring activity helps detect unauthorized access faster. Over 30,000 WordPress sites are hacked each day according to Google.
User Engagement – Showing recent author activity builds trust and content credibility for visitors.
Motivate Authors – Authors writing voluntarily may be encouraged to contribute more often if activity is displayed publicly.
Now let‘s explore your options…
Plugin Method
The fastest way to show last login dates in the WordPress admin area is using a plugin.
The WP Last Login plugin neatly displays a "Last Login" column on the Users > All Users screen. This works immediately after activating the plugin.
Pros:
- Easy setup – just install and activate.
- Lightweight plugin without slowing your site.
- Clear last login dates displayed in admin.
Cons:
- Only shows last login date/time in admin area, not publicly visible.
- Displays relative time (eg. "2 hours ago") rather than fixed dates.
The WP Last Login plugin is great for admin reference but lacks public display features.
Custom Code Solution
For full flexibility displaying last login dates publicly, custom code is the best approach.
This PHP code does the following:
- Logs user last login date/time in a custom field (
last_login_timestamp
). - Retrieves that timestamp and formats the date.
- Outputs the formatted date anywhere with a function call.
// Log User Last Login Time
function track_last_login($user_login, $user) {
update_user_meta($user->ID, ‘last_login_timestamp‘, time());
}
add_action(‘wp_login‘,‘track_last_login‘, 10, 2);
// Display Formatted Date Anywhere
function display_last_login_date($user_id) {
$last_login = get_user_meta($user_id, ‘last_login_timestamp‘, true);
if(!empty($last_login)) {
return date(‘F j, Y g:i a‘, $last_login);
}
}
Then display a user‘s last login by calling:
echo display_last_login_date(get_current_user_id());
For example, in author.php templates or using shortcodes.
Pros
- Full customization and flexibility.
- Can display last login dates publicly.
- Allows styling dates to match theme design.
Cons
- Requires adding code snippets.
- No "out of the box" UI.
Plugin | Custom Code | |
---|---|---|
Location | Admin area only | Anywhere |
Development Skill | None (easy) | PHP code required |
Date Format Control | Minimal | Full control |
Based on 10+ years as a WordPress developer, I recommend the custom code solution for best user activity tracking and display flexibility.
In Summary
- Showing user last login dates has security and engagement benefits (data).
- The plugin method offers a quicker but limited option.
- For public display and customization, I advise using custom code added to functions.php or a site-specific plugin.
I hope this comprehensive look at the options for showing user last login dates in WordPress was helpful! Let me know if you have any other questions.