Being able to quickly find the unique ID number of any user in your WordPress database can be very useful, especially if you run a website with lots of users. One of the ways that we can easily find a user’s ID is by customizing the table displayed on the wp-admin/users.php page. We can add a custom column that will display the ID number of every user.


This is a quick tip. Check out more Quick Tips

To add the user ID column, you can either copy this code into your functions.php, or add it as its own plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['user_id'] = 'User ID';
    return $columns;
}
 
add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
	if ( 'user_id' == $column_name )
		return $user_id;
    return $value;
}

Using the same methods as I have here, you could very easily add custom columns for even more user attributes, such as the user’s comment count.

  1. Spencer Hill

    Thanks for sharing this solution, is there a way to change the order of the column so the user ID can appear as the first column?

    • Pippin

      Yes, all you need to do is make the [‘user_id’] column be the first item in the array. Do you know how to do that?

  2. Steve

    Hi,

    Is there a way to show this column second (after the checkbox and ‘username’ column)?

    Also is it possible to make it sortable?

    • Pippin

      Yes, you can make it the second column. All of the columns shown on the user page are available in the $columns array, so all you have to do is set the “user_id” column to the second index. Does that make sense?

      For making it sortable, I’d suggest you read through this thread.

  3. Steve

    Hi Pippin,

    Thanks for your help. I think I have to split the array is this correct? I have no idea how to do that.

    I also wasn’t able to get the sorting working from that article.


    add_filter('manage_users_columns', 'pippin_add_user_id_column');
    function pippin_add_user_id_column($columns) {
    $columns['displayname'] = 'Display Name';
    return $columns;
    }
    add_filter( 'manage_users_sortable_columns', 'pippin_add_user_id_column' );

    function user_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'displayname' == $vars['orderby'] ) {
    $vars = array_merge( $vars, array(
    'meta_key' => 'displayname',
    'orderby' => 'meta_value',
    'order' => 'asc'
    ) );
    }
    return $vars;
    }
    add_filter( 'request', 'user_column_orderby' );

    function add_user_columns( $defaults ) {
    $defaults['displayname'] = __('Display Name', 'user-column');
    return $defaults;
    }

    function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'displayname' == $column_name )
    return $user->display_name;
    }
    add_action('manage_users_custom_column', 'pippin_show_user_id_column_content', 10, 3);
    add_filter('manage_users_columns', 'add_user_columns');

  4. Zeal

    How do i upate one particular user colum field on the database?

    • Pippin

      That’s not relevant to this tutorial, but you will need to look at the update_user_meta() function.

  5. ronnie

    hi pippin,
    Your solution is exclacty what I need!
    But I dont understand how to implement it..!
    tks! Ronnie

    • Pippin

      Placing this code in your functions.php of your active theme should do it for you.

  6. Kailey Lampert

    Be sure to always return $value in the manage_users_custom_column callback so you don’t accidentally overwrite other plugins’ columns.

    • Pippin

      Whoops! Fixed :)

  7. paulmarr

    Thanks pipin , thanks for the code. can you do one help . i need to make it ascending/descending like the columns username,email etc . please reply me.

    • Pippin

      That goes far beyond the scope of this tutorial, sorry.

  8. James

    How would you modify this code to return the ‘user_description’ field from get_user_meta? I’ve tried, but I keep getting the ID# or error messages:

    add_filter('manage_users_columns', 'custom_add_user_id_column');
    function custom_add_user_id_column($columns) {
    $columns['user_description'] = 'Company / Role';
    return $columns;
    }

    add_action('manage_users_custom_column', 'custom_show_user_id_column_content', 10, 3);
    function custom_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'user_description' == $column_name )
    return $user_id;
    return $value;
    }

    • Pippin

      Change “user_description” to “description”.

  9. BV

    Hi, I created a custom meta key with User Meta Manager plugin, but couldn’t get it to show in the column. The meta key is “company”, please kindly give me some advice, here’s the code I used:


    add_filter('manage_users_columns', 'pippin_add_user_company_column');
    function pippin_add_user_company_column($columns) {
    $columns['company'] = 'Company';
    return $columns;
    }

    add_action('manage_users_custom_column', 'pippin_show_user_company_content', 10, 3);
    function pippin_show_user_company_content($value, $column_name, $company) {
    $user = get_user_meta( $key );
    if ( 'company' == $column_name )
    return $key;
    return $value;
    }

    • Pippin

      Use this:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      
      add_filter('manage_users_columns', 'pippin_add_user_company_column');
      function pippin_add_user_company_column($columns) {
      	$columns['company'] = 'Company';
      	return $columns;
      }
       
      add_action('manage_users_custom_column', 'pippin_show_user_company_content', 10, 3);
      function pippin_show_user_company_content($value, $column_name, $user_id) {
      	if ( 'company' == $column_name )
      		return get_user_meta( $user_id, 'company', true );
      	return $value;
      }
    • BV

      Works nicely! Thanks so much Pippin!

  10. Dan

    Thanks for this. Worked like a charm. Do you know if it’s possible to pull in data from another table? I have a table that involves user ID and their current “points” on the site. I’d love to display those points in the column, but they aren’t located in wp_users or wp_usermeta,

    • Pippin

      You but you will have to use your own functions to retrieve the data.

    • Dan

      Thanks, I can certainly do that.

  11. Gabriel Reguly

    Thanks for sharing the code, where is the donate button?

    Regards,
    Gabriel

  12. Nathan Duckett

    Hi all,

    An old post but I found a easy solution for re-ordering the columns using unset()

    This is how I have done it with custom columns too:


    // unset default and set custom user columns
    function rm_manage_users_columns( $columns ) {
    unset( $columns );
    $columns = array(
    'cb' => 'cb',
    'custom_column' => 'Custom Column Name',
    'username' => 'Username',
    'custom_column_2' => 'Custom Column Name 2',
    'custom_column_3' => 'Custom Column Name 3',
    'custom_column_4' => 'Custom Column Name 4',
    );
    return $columns;
    }
    add_action( 'manage_users_columns', 'rm_manage_users_columns', 15, 1 );

    Tip: You can get the default column keys from viewing the source in your browser and looking at the column id attribute.

    Hope this helps,
    Nathan

  13. Umair

    Hi all,
    where we can add these changes? i have put these changes into include/admin.php and after update all of the changes was gone?

  14. Umair

    Thanks Pippin:

    August 23, 2012 at 8:17 pm
    Placing this code in your functions.php of your active theme should do it for you.

  15. Aswin Giri

    I used this code, I pasted it on function.php, User Id column is being displayed but id values are not being displayed, what might have happened?

  16. Denis

    How can I add the user ID column to my network admin User’s Table? (WP Multisite). This code only works for user tables outside of network admin.

    • Denis

      Nevermind, I found out. Gotta use: wpmu_users_columns and wpmu_users_custom_column hook.

  17. Gerry

    Thanks for a great post – got my ‘user_registered’ field added as a column.

    I’d like to make it sortable though … any ideas would be great

  18. Just

    Nice Jave script nice working