I have added a custom meta field to users called "customer_code". This all works and stores the information just fine.
I have added the "Customer Code" column to the users admin page like so:
add_filter('manage_users_columns', 'db_add_customer_code_column');
function db_add_customer_code_column($columns) {
$columns = array_merge(array('customer_code' => 'Customer Code'), $columns);
return $columns;
}
Again, this works just fine and the column appears on the users admin page.
However, I then have this code to populate the column with the "customer_code" meta data:
add_action('manage_users_custom_column', 'db_show_customer_code_column_content', 10, 3);
function db_show_customer_code_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
if ('customer_code' == $column_name){
$customer_code = get_the_author_meta( 'customer_code', $user_id );
return $customer_code;
return $value;
}
}
The column, however, remains completely blank!
I have used this exact same code on another site and it works perfectly there. It’s also definitely getting the customer code too because if I use "echo $customer_code" (instead of "return") then it correctly spits out all the customer codes across the top of the page; it just seems to be refusing to return each of them into the column for some reason!
Any ideas???
2
Answers
It looks like there was a conflict because something else (perhaps in the theme) is also calling the "manage_users_custom_column" filter.
Increasing my call's priority has fixed the issue (changed "10" to "15"):
There are some mistakes in your code. From the available user ID, you should better use WordPress
get_user_meta
function thanget_the_author_meta
function, to get and display custom user metadata.Also note that
manage_users_custom_column
is a filter hook, that requires to return the main argument$output
at the end, inside the related hooked function.Try the following instead:
It should work.
If I replace:
with:
I get something like:
So as you can see the column is populated, with the user’s first name. Be sure that
customer_code
is the right meta_key to be used, checking inwp_usermeta
database table.