Add custom meta column in the Users list
This is a very simple way to add a custom column to display in your user’s list. Say you want to show some preference that’s been added to the user’s profile with add_user_meta
First of all, let’s register the extra column
/**
* The function hooked to the filter hook manage_users_columns that simply
* adds a new column by key and value to the existing core columns for the users list
*
* @author Elliott Richmond <me@elliottrichmond.co.uk>
* @since 0.1.0
* @param array $columns Existing columns array.
* @return string $columns The new columns array.
*
*/
function wpstx_custom_user_columns($columns) {
$columns['select_role'] = 'Preference';
return $columns;
}
add_filter('manage_users_columns', 'wpstx_custom_user_columns', 10, 1);
Next, we’ll add the data that you want to show when that row is rendered, if the column name is equal to the key we provided above in the manage_users_colums
hook we’ll use get_user_meta
to get the data we want to show by the users ID
and return the value.
/**
* The function hooked to the filter hook manage_users_custom_column that displays
* the information we want to see by the in individual user by ID or row listed
*
* @author Elliott Richmond <me@elliottrichmond.co.uk>
* @since 0.1.0
* @param string $val Existing columns array.
* @param string $column_name The key value of the column that we will switch to set in our function 'wpstx_custom_user_columns'.
* @param int $user_id The users ID used to identify the row the list is going to output.
*
* @return string $val The existing or new string we want to display in the users list.
*
*/
function wpstx_custom_user_row( $val, $column_name, $user_id ) {
switch ($column_name) {
case 'select_role' :
$meta_key = get_user_meta($user_id, 'select_role', true);
$val = wpstx_select_role_output($meta_key);
return $val;
default:
}
return $val;
}
add_filter( 'manage_users_custom_column', 'wpstx_custom_user_row', 10, 3 );
You’ll notice I’m also using a function on the meta value, this is just a simple helper function to change the output by the key saved in the user’s meta, rather than use my function you could simply return $meta_key
instead.
/**
* This is an extra function to change a lowercase meta key
* from the standard WordPress way of storing keys and changing
* them to our prefered value as defined by the key value
*
* @author Elliott Richmond <me@elliottrichmond.co.uk>
* @since 0.1.0
* @param string $meta_key The incoming meta key.
* @return string $preference The output text.
*
*/
function wpstx_select_role_output($meta_key) {
// die early if empty
if ($meta_key == '') {
return;
}
$output = array(
'subscriber' => 'Recieve snippets',
'contributor' => 'Submit snippets',
);
$preference = $output[$meta_key];
return $preference;
}
@source https://github.com/eirichmond/wpstx-user-columns.git