Show Last login Time Of User In WordPress

How to display Quickly a WordPress user’s last login with custom code .

Actually some time  we need to know our active users of site. so we can differentiate which user hasn’t logged in for a while.

This is combination  4-5 hooks.

1: First we can use wp_login hook for saving login time and date.

<?php
//Record user's last login to custom meta field
add_action( 'wp_login', 'custum_wp_login', 10, 2 );

function custum_wp_login( $user_login, $user ) {
    update_user_meta( $user->ID, 'custum_last_login', date('Y-m-d H:i:s'));
}

 

2: Second we need to add column in user table to show last login

<?php
//Register new custom column with last login time
add_filter( 'manage_users_columns', 'custum_manage_users_columns');
add_filter('manage_users_custom_column',  'custum_manage_users_custom_column', 10, 3);

function custum_manage_users_columns( $columns )
{
    $columns['custum_last_login'] = __('Last login', 'last_login');
    return $columns;
}
function custum_manage_users_custom_column( $value, $column_name, $user_id )
{
    if ( 'custum_last_login' == $column_name){
     $last_login = get_user_meta( $user_id, 'custum_last_login', true );
         $value = date("g:i a - d-M-y", strtotime( $last_login));
    }
    return $value;
}

 

After adding this code you’ll see a new last login column.

 

Related Blogs

WooCommerce: Check if Product Category is in the Cart

To check if a specific product category is in the cart during the `woocommerce_before_cart` action hook, you can use a similar approach as in the previous answer. However, in this case, you need to use the `woocommerce_before_cart_contents` action hook, which fires before the cart items are displayed on the cart page. Here’s how you can achieve this:

Shortcodes included with WooCommerce

WooCommerce comes with several shortcodes that you can use to display various elements and functionalities on your WordPress website. These shortcodes allow you to customize the appearance and layout of your WooCommerce store. Here are some of the essential shortcodes included with WooCommerce:

Request A Quote