User Last Login Sort Column


I’ve seen there are several ways to display a user’s last login date on the Users page without having to use a 3rd party plugin. Building on that, I’d really like to have the user’s last login date column be sortable (hopefully orderby date).

I’m using the display user last login function via wpdailybits, but my additions to the code can’t get the column to sort.

Suggestions? I know I’m missing something…

//Capture User Last Login
    add_action('wp_login','wpdb_capture_user_last_login', 10, 2);
    function wpdb_capture_user_last_login($user_login, $user){
        update_user_meta($user->ID, 'last_login', current_time('mysql'));

//Display Last Login Date in Admin
    add_filter( 'manage_users_columns', 'wpdb_user_last_login_column');
    function wpdb_user_last_login_column($columns){
        $columns['lastlogin'] = __('Last Login', 'lastlogin');
        return $columns;

    add_action( 'manage_users_custom_column',  'wpdb_add_user_last_login_column', 10, 3); 
    function wpdb_add_user_last_login_column($value, $column_name, $user_id ) {
        if ( 'lastlogin' != $column_name )
            return $value;

        return get_user_last_login($user_id,false);

    function get_user_last_login($user_id,$echo = true){
        $date_format = get_option('date_format') . ' ' . get_option('time_format');
        $last_login = get_user_meta($user_id, 'last_login', true);
        $login_time = 'Never logged in';
                $login_time = mysql2date($date_format, array_pop($last_login), false);
                $login_time = mysql2date($date_format, $last_login, false);
            echo $login_time;
            return $login_time;
//Sort the Columns
    add_filter( 'manage_edit-users_sortable_columns', 'sortable_users_column' );
    function sortable_users_column( $sortable_columns  ) {

        $sortable_columns[ 'last_login_column' ] = 'lastlogin';

timmyg 2 months 0 Answers 20 views 0

Leave an answer