Web Design Web Development WordPress

How to: Add new column on user listing of WordPress Dashboard

Today we shall talk about adding a new column to your WordPress’s User Listing table in the back end. WordPress by default has “Gravatar”, “username”, “Name”, “Email”, “Role” and “Posts” listed in the users table. But what if we have to add new custom column to the list? Say you run a membership website, and want to show when the account of that particular user expires. Or may be you want to show some other info about the user in the newly added column. This can be attained by using adding a “manage_users_columns” filter in your theme’s functions.php or your plugin’s file. Here is a sample code, which should be pretty self explanatory.

33333

// we add a filter that call a function: “admin_color_column”
add_filter(‘manage_users_columns’, ‘admin_color_column’);

//The main function that adds the column header.
function admin_color_column($columns) {
$new = array();
foreach($columns as $key => $title) {
/*
Put the Admin Color column before the Author column. You can change this to where you want to show your column
*/
if ($key==’username’)
$new[‘admin_color’] = ‘Admin Color’; // Our custom column’s identifier and text
$new[$key] = $title;
}
return $new;
}

// Add “manage_users_custom_column” filter to show data in the newly added column
add_filter( ‘manage_users_custom_column’, ‘show_admin_color’, 10, 3 );
function show_admin_color( $val, $column_name, $user_id ) {
// Get user admin color. You can show whatever you want here.
$color = get_user_meta($user_id,’admin_color’,true);
switch ($column_name) {
// check if we are under “admin_color” column
case ‘admin_color’ :
return $color;
break;

default:
}
return $return;
}

 

With this you should now have a new column added to your user’s listing table that reads “Admin Color”, with their choice of color listed below. (if you are wondering, what Admin Color is, This is Dashboard theme color; which can be changed from your User Profile.)