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.
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?
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?
Hi,
Is there a way to show this column second (after the checkbox and ‘username’ column)?
Also is it possible to make it sortable?
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.
I’d also recommend this article, which has a very thorough step-by-step tutorial on how to accomplish this. I learned a lot, too, as opposed to simply copying the code and pasting it in.
https://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable–wp-25095
Here’s my code, for reference.
// Register the column as sortable
add_filter( ‘manage_users_sortable_columns’, ‘my_sortable_sales_rep_column’ );
function my_sortable_sales_rep_column( $columns ) {
$columns[‘sales_rep’] = ‘sales_rep’;
//To make a column ‘un-sortable’ remove it from the array
//unset($columns[‘date’]);
return $columns;
}
add_action( ‘pre_get_posts’, ‘my_sales_rep_orderby’ );
function my_sales_rep_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( ‘orderby’);
if( ‘sales_rep’ == $orderby ) {
$query->set(‘meta_key’,’sales_rep’);
$query->set(‘orderby’,’meta_value’);
}
}
Hope this helps! 🙂
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');
Probably the easiest way would be to sort the $columns array. See here for some examples: http://php.net/manual/en/array.sorting.php
How do i upate one particular user colum field on the database?
That’s not relevant to this tutorial, but you will need to look at the update_user_meta() function.
hi pippin,
Your solution is exclacty what I need!
But I dont understand how to implement it..!
tks! Ronnie
Placing this code in your functions.php of your active theme should do it for you.
Be sure to always return
$value
in themanage_users_custom_column
callback so you don’t accidentally overwrite other plugins’ columns.Whoops! Fixed 🙂
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.
That goes far beyond the scope of this tutorial, sorry.
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;
}
Change “user_description” to “description”.
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;
}
Use this:
Works nicely! Thanks so much Pippin!
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,
You but you will have to use your own functions to retrieve the data.
Thanks, I can certainly do that.
Thanks for sharing the code, where is the donate button?
Regards,
Gabriel
Never mind, I have just subscribed to the site 🙂
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
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?
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.
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?
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.
Nevermind, I found out. Gotta use: wpmu_users_columns and wpmu_users_custom_column hook.
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
Nice Jave script nice working
Hi All,
Great work, i have included my custom field using this method, now i want to allow the admin to search the list of users using my newly created field,
Any help would be appreciated.
Lot of thanks, now it’s working…..
hi ,nice coding,i have created a colum named as company and i want to display company name,when i have submitted an form with field as company.any options??
Hi, I am not a coder, I had someone make a site and I need to have the phone number column show in the users page so I can identify which users have included their numbers when registering as a member. I want to be able to sort the column so I can see all together the number of people with phone numbers. I know I can export the list into excel and work outside however it would be good to have this functionality. Can anyone help me please.
Hello, I know that this is a very old post but I would appreciate it if anybody could help.. I have used the above snippet that adds this new column in wordpress dashboard. But how could I search users in wordpress dashboard by their id???? Thank you!!
The following code is working to make the User ID column sortable:
// Register the column as sortable
function status_column_sortable($columns) {
$custom = array(
‘user_id’ => ‘ID’,
);
return wp_parse_args($custom, $columns);
}
add_filter( ‘manage_users_sortable_columns’, ‘status_column_sortable’ );
Hello,
Instead of User ID, how could I get the users phone number in a column?
Thank you!
Hi
This post is a long time ago but surfing the internet I haven´t managed to find the solution, do you know if it´s possible to add a prefix, a suffix (or both) to the WordPress User ID ?.
Many Thanks and Congratulations on the web!
A greeting
Does this method still work? I was trying to get the User ID column to show up in my Comments list but it’s not showing up.
Thanks!
This code is for showing the user ID on the users’ table. Showing it on the Comments list will require a few changes.
This tutorial will get you close to what you need: https://rudrastyh.com/wordpress/comments-table-columns.html
Oops, no wonder! It DOES work! Thanks for setting me straight. I was able to add a new column to the Comment table, but adding the user ID field is a different story….
I want to be able to sort the column so I can see all together the number of people with phone numbers. I know I can export the list into excel and work outside however it would be good to have this functionality