Pawan Kumawat

How to Display Last Login Time of Users in WordPress

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.

 

  1. Understanding the Importance of Showing Last Login Time
    • Improving user engagement and trust
    • Enhancing transparency and accountability
    • Strengthening security measures
  2. Using WordPress Functions to Track Last Login Time
    • Utilizing the wp_login action hook
    • Accessing user information with get_user_by()
    • Updating user meta with update_user_meta()
  3. Retrieving and Formatting Last Login Time
    • Retrieving the last login time using get_user_meta()
    • Formatting the time using date_i18n()
    • Handling cases when last login time is not available
  4. Displaying Last Login Time in Your WordPress Theme
    • Creating a function to display the last login time
    • Incorporating the function in your theme templates
    • Styling the display to match your site’s design
  5. Customizing the Functionality for Advanced Usage
    • Restricting the display to specific user roles
    • Modifying the output to include additional user information
    • Extending the functionality using WordPress filters and actions
  6. Optimizing Meta Key for SEO and Readability
    • Choosing a relevant meta key for the last login time
    • Considering SEO best practices for meta information
    • Implementing a user-friendly and descriptive meta key
  7. Handling Privacy and Data Protection Concerns
    • Respecting user privacy preferences
    • Complying with data protection regulations
    • Clearing last login time for deactivated or deleted users
  8. Testing and Debugging for Seamless Functionality
    • Performing thorough testing across different user scenarios
    • Debugging common issues with last login time display
    • Ensuring compatibility with various WordPress versions

 

 

 

Categories

Related Blogs

WooCommerce: Display Regular & Sale Price @ Cart Table

Learn how to enhance your WooCommerce cart table by displaying both the regular and sale prices. Implement this functionality using WordPress hooks and filters to provide clear and appealing pricing information to your customers, encouraging sales and improving the shopping experience.