Last login column for users
Remembers when each user last logged in and shows it in the users list. Useful for finding accounts nobody uses any more.
<?php
add_action(
'wp_login',
static function ( $login, $user ) {
update_user_meta( $user->ID, 'snipfire_last_login', time() );
},
10,
2
);
add_filter(
'manage_users_columns',
static function ( $columns ) {
$columns['snipfire_last_login'] = 'Last login';
return $columns;
}
);
add_filter(
'manage_users_custom_column',
static function ( $output, $column, $user_id ) {
if ( 'snipfire_last_login' !== $column ) {
return $output;
}
$time = (int) get_user_meta( $user_id, 'snipfire_last_login', true );
return $time ? esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time ) ) : '—';
},
10,
3
);
Good to know
Starts counting when you switch it on: earlier logins show as “—”.
In these packs
- Users and profiles: Registration dates, last logins, social profile fields, longer sessions and stricter passwords.