Add Role and Last Login columns to the WordPress Users table.
The snippet automatically records the date and time a user logs in and displays it directly in Users → All Users. It also shows the user’s assigned WordPress role, making it easier for administrators to review user accounts without opening each profile individually.
Users who haven’t logged in since the snippet was added appear as “—”.
Steps
- Add the snippet to your site’s custom plugin or theme’s
functions.phpfile. - Log in to WordPress with a user account.
- Go to Users → All Users.
- You will see two additional columns:
- Role — Displays the user’s assigned WordPress role.
- Last Login — Displays the user’s most recent login date and time.
- When another user logs in, their Last Login value is automatically updated.
- If a user has never logged in after the snippet was installed, the Last Login column displays —.
Benefits
- Quickly see user roles from the main Users screen.
- Track when users last accessed the website.
- Identify inactive or unused user accounts more easily.
- Useful for security and user-account audits.
- Helps administrators manage websites with many users.
- Eliminates the need to open individual user profiles to check login information.
- Automatically records login activity without requiring an additional plugin.
- Can be useful for membership, community, client, and editorial websites.
/**
* Add Role and Last Login columns to the WordPress Users table.
*
* Stores the user's last login timestamp and displays it
* in the WordPress Users admin screen.
*/
/**
* Store the user's last login time.
*
* @param string $user_login User login name.
* @param WP_User $user User object.
*/
function wpfolks_store_last_login( $user_login, $user ) {
update_user_meta(
$user->ID,
'_last_login',
current_time( 'mysql' )
);
}
add_action( 'wp_login', 'wpfolks_store_last_login', 10, 2 );
/**
* Add custom columns to the Users table.
*
* @param array $columns Existing columns.
* @return array
*/
function wpfolks_add_user_columns( $columns ) {
$columns['user_role'] = __( 'Role', 'wpfolks' );
$columns['last_login'] = __( 'Last Login', 'wpfolks' );
return $columns;
}
add_filter( 'manage_users_columns', 'wpfolks_add_user_columns' );
/**
* Display content for custom user columns.
*
* @param string $output Column output.
* @param string $column_name Column name.
* @param int $user_id User ID.
* @return string
*/
function wpfolks_user_column_content( $output, $column_name, $user_id ) {
if ( 'user_role' === $column_name ) {
$user = get_userdata( $user_id );
if ( $user && ! empty( $user->roles ) ) {
$roles = array_map(
function ( $role ) {
return translate_user_role( $role );
},
$user->roles
);
return esc_html( implode( ', ', $roles ) );
}
return '—';
}
if ( 'last_login' === $column_name ) {
$last_login = get_user_meta(
$user_id,
'_last_login',
true
);
if ( empty( $last_login ) ) {
return '—';
}
return esc_html(
wp_date(
get_option( 'date_format' ) . ' '. get_option( 'time_format' ),
strtotime( $last_login )
)
);
}
return $output;
}
add_filter(
'manage_users_custom_column',
'wpfolks_user_column_content',
10,
3
);
Tested on
WordPress 7.1.2
Reported by the author on September 29, 2026. WPFolks has not verified this.