How to add sortable custom column on users listing screen in WordPress

WordPress users listing screen shows very limited data. So how about showing some more information like user id for each other. There may be even some more useful info that you would like to show like any custom user meta data. Then we think of, is this really doable and if so, how?

The answer will be a big YES, thanks to the filters provided by WordPress. One such filter we will be using is manage_users_columns. This contains all the user columns. So when we will hook this one, then the associated function will add one more column to the default columns. To make the column sortable, we will hook the same function with manage_users_sortable_columns.

This is how we will be using this hook.

add_filter( 'manage_users_columns', 'wti_add_user_custom_column' );
add_filter( 'manage_users_sortable_columns', 'wti_add_user_custom_column' );

function wti_add_user_custom_column( $columns ) {
    $new_columns = $columns + array( 'user_id' => 'ID' );
    return $new_columns;

The above code will add a column but it will come on the last place. So how about placing this column at a different position, may be somewhere like before Username column? Let’s see how the original user columns array contain.

    [cb] => 
    [username] => Username
    [name] => Name
    [email] => E-mail
    [role] => Role
    [posts] => Posts

In the above array, cb contains the checkboxes which we see as the 1st column. With a bit of logic to the above function, we can insert one key => value pair between cb and username. Let’s change the above logic to accomplish this.

function wti_add_user_custom_column( $columns ) {
    $new_columns = array();
    $columns_1 = array_slice( $columns, 0, 1 );
    $columns_2 = array_slice( $columns, 1 );
    $new_columns = $columns_1 + array( 'user_id' => 'ID' ) + $columns_2;
    return $new_columns;

This is what we have done here.

– We have sliced the user columns array into 2 pieces.
– Since we will be placing the new column before Username, we will slice the array at the very 1st position (0th index) and that array will contain the 1st column only (the column having checkboxes).
– The 2nd array will have rest of the columns starting from Username.
– Now we will combine the above 2 arrays with the new custom column array inserted in between them.

That’s it. Now we have a column at a custom position. You can also place this at any position by just changing array_slice index.

The next thing we will do is retrieving the user id and show it under User ID column. We will take the help of another hook manage_users_custom_column.

add_action( 'manage_users_custom_column', 'wti_show_user_custom_column_content', 10, 3);

function wti_show_user_custom_column_content( $value, $column_name, $user_id ) {
	if ( 'user_id' == $column_name )
		return $user_id;
	return $value;

We want to show the user id and we already have that as argument of this function. We will just if the column is user id column, then we will just return the value there. Now you reload the users screen and you must see a new column at 2nd position showing the user id of each user.

That’s it. Happy coding :)

  1. 3 years ago

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>