Create Custom Menu in WordPress Dashboard
Complete tutorial on create custom menu in wordpress dashboard. Discover practical examples, implementation tips, and expert advice for WordPress and WooCo
Read More โDisplay user last login time in WordPress by hooking into wp_login action and storing timestamps with user meta. Show login history to users for transparency, security awareness, and account monitoring with formatted date display.
You can display a user's last login time in WordPress by hooking into the login process and saving the login timestamp. Here's how to set this up: Step 1: Open your theme’s functions.php file. Step 2: Add the following code snippet:
// Record last login time
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);
// Display last login time
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 = date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($last_login));
echo 'Last login: ' . $formatted_date;
}
}
}
Step 3: Save the file and upload it. Step 4: Use this line where you want the login time displayed:
<?php display_last_login(); ?>
wp_loginget_user_by(), update_user_meta()date_i18n()last_login) 
Search our archives or reach out to our team for solutions and expert advice.