The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Add Custom Columns to Users Showing Role and Last Login

Shared by Alkesh Miyani · September 29, 2026 · @wp_login

8 views
2 upvotes
Back to Snippets

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

  1. Add the snippet to your site’s custom plugin or theme’s functions.php file.
  2. Log in to WordPress with a user account.
  3. Go to Users → All Users.
  4. 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.
  5. When another user logs in, their Last Login value is automatically updated.
  6. If a user has never logged in after the snippet was installed, the Last Login column displays —.

Benefits

/**
 * 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.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

1 comment

  1. Amit Dholiya
    Amit Dholiya @amitdholiya

    snippet

    Report