How to display multiple custom columns in the wp-admin users.php?

I’m using the manage_users_columns to display a custom field I create in the usermeta database called company. My code is as follows:

function mysite_column_company( $defaults ) {
 $defaults['mysite-usercolumn-company'] = __('Company', 'user-column');
  return $defaults;
 }
 function mysite_custom_column_company($value, $column_name, $id) {
  if( $column_name == 'mysite-usercolumn-company' ) {
        return get_usermeta($id, 'company');
  }
 }

add_action('manage_users_custom_column', 'mysite_custom_column_company', 15, 3);
add_filter('manage_users_columns', 'mysite_column_company', 15, 1);

I’d like to get to display two other custom fields, but can’t figure out the proper function. Duplicating this one doesn’t work and I’ve not had any luck adding new functions within this one for each unique column, mostly because I can’t seem to figure out how to properly define the $defaults variable. Any suggestions?

Related posts

Leave a Reply

1 comment

  1. hummm… do you mean something like this :

    function mysite_column_company( $defaults ) {
        $defaults['mysite-usercolumn-company'] = __('Company', 'user-column');
        $defaults['mysite-usercolumn-otherfield1'] = __('Other field 1', 'user-column');
        $defaults['mysite-usercolumn-otherfield2'] = __('Other field 2', 'user-column');
        return $defaults;
    }
    function mysite_custom_column_company($value, $column_name, $id) {
        if( $column_name == 'mysite-usercolumn-company' ) {
            return get_the_author_meta( 'company', $id );
        }
        elseif( $column_name == 'mysite-usercolumn-otherfield1' ) {
            return get_the_author_meta( 'otherfield1', $id );
        }
        elseif( $column_name == 'mysite-usercolumn-otherfield2' ) {
            return get_the_author_meta( 'otherfield2', $id );
        }
    }
    

    by the way, you should use get_the_author_meta() instead of get_usermeta() as it is deprecated.