Want to show when a user last logged in? WordPress doesn’t include this by default, but you can add it using hooks and a simple meta key.
Step 1: Edit your active theme’s functions.php
file.
Step 2: Add this code snippet:
// Capture last login on login event function update_last_login($user_login) { $user = get_user_by('login', $user_login); $current_time = current_time('mysql'); update_user_meta($user->ID, 'last_login', $current_time); } add_action('wp_login', 'update_last_login', 10, 2); // Output last login timestamp function display_last_login() { if (is_user_logged_in()) { $user = wp_get_current_user(); $last_login = get_user_meta($user->ID, 'last_login', true); if ($last_login) { $formatted = date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($last_login)); echo 'Last login: ' . $formatted; } } }
Step 3: Insert this line into your theme where you want to show the login time:
<?php display_last_login(); ?>
Why Use This?
- Shows users when they last signed in
- Enhances security monitoring
- Helps admins track user activity
Pro Tip: Customize output for user roles, profile pages, or even show in admin tables using admin_init
or user_contactmethods
filters.