How to add a Sortable User Registration Date Column to the All Users page

Example: let’s suppose that there was a spam attack on your website and there were registered near 50 spam users. But your website has 1000 users. How to remove the recent 50 users without going directly to database?

And here is the solution.

Here I made the default Posts column hidden and added a Registration date column which is sortable as well.
On this screenshot the default Posts column is hidden, but the Registration Date column is added. And when you click on its title, the users in this table will be sorted by their registration date — descending or ascending (click the title twice).

The following code is ready to use, you can insert it even without changes to your current WP theme functions.php file.


/*
 * Create a column. And maybe remove some of the default ones
 * @param array $columns Array of all user table columns {column ID} => {column Name} 
 */
add_filter( 'manage_users_columns', 'rudr_modify_user_table' );

function rudr_modify_user_table( $columns ) {
	
	// unset( $columns['posts'] ); // maybe you would like to remove default columns
	$columns['registration_date'] = 'Registration date'; // add new

	return $columns;

}

/*
 * Fill our new column with the registration dates of the users
 * @param string $row_output text/HTML output of a table cell
 * @param string $column_id_attr column ID
 * @param int $user user ID (in fact - table row ID)
 */
add_filter( 'manage_users_custom_column', 'rudr_modify_user_table_row', 10, 3 );

function rudr_modify_user_table_row( $row_output, $column_id_attr, $user ) {
	
	$date_format = 'j M, Y H:i';

	switch ( $column_id_attr ) {
		case 'registration_date' :
			return date( $date_format, strtotime( get_the_author_meta( 'registered', $user ) ) );
			break;
		default:
	}

	return $row_output;

}

/*
 * Make our "Registration date" column sortable
 * @param array $columns Array of all user sortable columns {column ID} => {orderby GET-param} 
 */
add_filter( 'manage_users_sortable_columns', 'rudr_make_registered_column_sortable' );

function rudr_make_registered_column_sortable( $columns ) {
	return wp_parse_args( array( 'registration_date' => 'registered' ), $columns );
}

Line 9
This piece of code helps you to remove any of the default columns, these columns are: name, email, role, posts but remember that you can just hide some of these columns in «Screen Options».
Line 26
Date format to display. You can use any format that supported by date() PHP function.
Line 30
Here get_the_author_meta() function returns the date user registered in the same format it is stored in database: 2017-02-09 05:37:39
Line 46
We need wp_parse_args() to merge two arrays. If $columns array already contains the element with registration_date key, this element value will be updated.

If the column haven’t appeared in your WordPress admin area on the Users > All Users page or maybe you would like to hide the other columns without code — try to use «Screen Options» tab then.

You can turn on or turn off the specific columns on the Screen Options tab
You can turn on or turn off the specific columns on the Screen Options tab

If you have any questions left I’m ready to help you in comments.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter