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 โImplement last login tracking in WordPress using wp_login action hook triggered on user authentication. Update user meta with current_time when user logs in, store timestamp in last_login meta key using update_user_meta function, create display function checking if user logged in with is_user_logged_in, retrieve current user with wp_get_current_user, get last login timestamp from user meta with get_user_meta, format date with date_i18n using WordPress date and time format settings, display formatted login time on user dashboard or profile pages, implement user activity monitoring, enhance security by showing account access times, and provide transparency for users monitoring their account activity.
To display the last login time of a user in WordPress, you can use a combination of functions and hooks. Here's an example of how you can achieve this: Step 1: Open your theme's `functions.php` file. Step 2: Add the following code to the file:
// Function to update 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);
// Function to 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 to your theme directory. Step 4: Now, you can place the following code wherever you want to display the last login time in your WordPress theme:
<?php display_last_login(); ?>
With these steps, the `update_last_login()` function will be triggered whenever a user logs in, updating the user meta information with the current login time. The `display_last_login()` function checks if the user is logged in and retrieves the last login time from the user meta. It then formats and displays the last login time using the date and time format settings from your WordPress dashboard.
wp_login action hookget_user_by()update_user_meta()get_user_meta()date_i18n()Search our archives or reach out to our team for solutions and expert advice.