Display user’s last login time.

Hiển thị lần đăng nhập cuối cùng của người dùng

This article provides a solution for tracking and displaying the last login time of users on a website with user management features. By adding code to the functions.php file, users’ last login data can be collected and displayed in a new column on the “All Users” page. Additionally, the tutorial includes steps for making the data column sortable, allowing users to easily see who has logged in recently. This feature is useful for managing users on community development websites. Readers are encouraged to share and comment on the article for more WordPress tips and knowledge.

Are you working on a website with user management features and wondering about the last login time of a user? If so, this article is perfect for you. After reading this, you’ll know how to store and display the user’s last login information.

Stores the user’s last login

To begin, WordPress doesn’t automatically store the last login data, so we need to do it ourselves. There are two ways users log in: through the ‘wp-login.php’ form or the ‘wp_signon()’ function. Fortunately, the ‘wp_login’ action hook works for both cases.

add_action( 'wp_login', 'hk_collect_login_timestamp', 20, 2 );

function hk_collect_login_timestamp( $user_login, $user ) {

    update_user_meta( $user->ID, 'last_login', time() );

}

Add this code to the ‘functions.php’ file of your theme or child theme to start collecting the user’s last login data.

Display data

Next, let’s create a column on the "All Users" page to show the last login information. Use the following code:

add_filter( 'manage_users_columns', 'hk_add_last_login_column' );
add_filter( 'manage_users_custom_column', 'hk_last_login_column', 10, 3 );

function hk_add_last_login_column( $columns ) {

    $columns['last_login'] = 'Last Login';
    return $columns;

}

function hk_last_login_column( $output, $column_id, $user_id ){

    if( $column_id == 'last_login' ) {

        $last_login = get_user_meta( $user_id, 'last_login', true );
        $date_format="j M, Y";

        $output = $last_login ? date( $date_format, $last_login ) : '-';

    }

    return $output;

}

After adding this code, your user table will now include a new column labeled "Last Login."

See also  Changing PHP parameters on hosting using cPanel and Directadmin

Make data columns sortable

To make it easier to see who has logged in recently, let’s make the "Last Login" column sortable. Use the following code to enable sorting by the last login date:

add_filter( 'manage_users_sortable_columns', 'hk_sortable_columns' );
add_action( 'pre_get_users', 'hk_sort_last_login_column' );

function hk_sortable_columns( $columns ) {

    return wp_parse_args( array(
        'last_login' => 'last_login'
    ), $columns );

}

function hk_sort_last_login_column( $query ) {

    if( !is_admin() ) {
        return $query;
    }

    $screen = get_current_screen();

    if( isset( $screen->id ) && $screen->id !== 'users' ) {
        return $query;
    }

    if( isset( $_GET[ 'orderby' ] ) && $_GET[ 'orderby' ] == 'last_login' ) {

        $query->query_vars['meta_key'] = 'last_login';
        $query->query_vars['orderby'] = 'meta_value';

    }

    return $query;

}

Epilogue

I hope this article has provided you with valuable insights on managing user data effectively, especially for community-driven websites. If you found this article helpful, please consider sharing it and leaving a comment. For more WordPress tips, you can follow our Facebook page.

Rate this post

Related posts