How To Add a Sortable User Registration Date Column to All Users Page

Sorting users by registration date may be necessary in a variety of WordPress scenarios. For instance, you can discover that spam users have recently registered on your website, and since your users list is by default arranged alphabetically, it can be challenging to locate them.

As seen in the screenshot below, we are going to build a “Registration date” column in this lesson. More importantly, though, we will make it sortable.

this is how to sort users by registration date in WordPress admin

We only need to use three WordPress filter hooks to provide this feature; we don’t need to use any plugins, though we could of course:

  • manage_users_columns – to add a columnt to All Users page,
  • manage_users_custom_column – to display the registration date inside it,
  • manage_users_sortable_columns – to make it sortable.

 

/*
 * Creating a column (it is also possible to remove some default ones)
 */
add_filter( 'manage_users_columns', 'zalweb_modify_user_table' );
function zalweb_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 registration dates of the users
 */
add_filter( 'manage_users_custom_column', 'zalweb_modify_user_table_row', 10, 3 );
function zalweb_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 : {
			break;
		}
	}

	return $row_output;

}

/*
 * Make our "Registration date" column sortable
 */
add_filter( 'manage_users_sortable_columns', 'zalweb_make_registered_column_sortable' );
function zalweb_make_registered_column_sortable( $columns ) {
	
	return wp_parse_args( array( 'registration_date' => 'registered' ), $columns );
	
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *