Trying to get the last login of each user and display it into the admin panel. Currently I can display the first user within the admin loop, however I can't pull out the remaining users.

I think I need to loop through and pull out each user meta_value where it's equal to last_login but I've tried a standard query and its not working. Code is:

    function modify_user_column( $column_name, $user_id ) {

//get current user object
    $current_user = wp_get_current_user();

    if( 'last_login' != $column_name ) {

            return get_last_login( $current_user->ID );

add_action( 'manage_users_custom_column', 'modify_user_column', 10, 2 );

And get_last_login() is:

    function get_last_login($user_id) {

   $last_login = get_user_meta($user_id, 'last_login', false);

  $last_login = get_user_meta($user_id, 'last_login', true);
$date_format = get_option('date_format') . ' ' . get_option('time_format');
$the_last_login = mysql2date($date_format, $last_login, false);
return $the_last_login;



1 Answer 1


This will add a new column to the Users admin and show their last login.

/* Plugin Name: (#158276) WPSE | Last user login */

// Add user meta `last_login` that saves the UNIX time stamp 
// to identify the exact time when a user logged in
add_action( 'wp_login', 'add_login_time' );
function add_login_time( $user_login ) {
    global $wpdb;
    $user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = %s", $user_login ) );
    update_user_meta( $user_id, 'last_login', current_time('mysql') );

// Add a new column to an admin list table
add_filter( 'manage_users_columns', 'add_last_login_column' );
function add_last_login_column( $columns ) {
    $columns['last_login'] = __( 'Last login', 'last_login' );
    return $columns;

// Contents of the new admin list table column
add_action( 'manage_users_custom_column',  'add_last_login_column_value', 10, 3 );
function add_last_login_column_value( $value, $column_name, $user_id ) {
    $meta = get_user_meta( $user_id, 'last_login', true );
    if ( 'last_login' == $column_name && $meta ) {
        return date_i18n( 
                '%s - %s',
                get_option( 'date_format' ),
                get_option( 'time_format' )
            strtotime( $meta ),
            get_option( 'gmt_offset' )
    return $value;
  • +1, Darren. I reworked your answer slightly and added some comments. I also tried to reduce the amount of database queries. I hope you have time to review my changes. Sidenote: I also have taken the date_i18n() function, which should work for internationalization and added the gmt_offset to it.
    – kaiser
    Aug 18, 2014 at 16:52
  • @kaiser I'm afraid this doesn't work for me; it doesn't populate the Last Login column with a date/time. I enabled debugging but it didn't return any errors.
    – D-B
    Aug 20, 2014 at 7:29
  • Could you dump the three differwnt values for date_i18n()?
    – kaiser
    Aug 20, 2014 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.