Use custom user fields in the list of users in WordPress

4

I have installed a plugin in wordpress that allows inserting registration forms for users with custom fields (in my case, "Company" and "CIF")

The insertion of data is done correctly, but I also want to show them in the users section as in the screenshot that I attached:

To do this, I am using the following functions that I add to the file functions.php :

function theme_add_user_company_column( $columns ) {

     $columns['company'] = __( 'Compañía', 'theme' );
     return $columns;
 } 
 add_filter( 'manage_users_columns', 'theme_add_user_company_column' );

With this I have created the column that is displayed in Users

function theme_show_user_company_data( $value, $column_name, $user_id ) {

     if( 'company' == $column_name ) {

         return get_user_meta($user_id , 'company', true );
     } // end if

 } // end theme_show_user_zip_code_data
 add_action( 'manage_users_custom_column', 'theme_show_user_company_data', 10, 3 );

With this other I have filled the column with the records that I have entered in the form.

The problem I have is that when trying the same for CIF, the company data stops appearing. I can only show one type of data each time and I do not know why it can be.

    
asked by Alfonso 02.02.2016 в 20:07
source

2 answers

1

The code that you use to create the CIF column is missing. Assuming that it is an exact copy of the one you use for the company column, I think the problem is that you are adding twice the share manage_users_custom_column , with which the second "overwrites" the first ...

I think you would need something like:

function theme_show_user_custom_columns_data( $value, $column_name, $user_id ) {

  if ('company' == $column_name) {
    return get_user_meta($user_id, 'company', true );

  } elseif ('cif' == $column_name) {
    return get_user_meta($user_id, 'cif', true );
  }
}
add_action( 'manage_users_custom_column', 'theme_show_user_custom_columns_data', 10, 3 );

Anyway, I have not used WordPress for a long time and I could be totally wrong:)

    
answered by 03.02.2016 в 17:27
1

Try the following code:

// Crea las columnas
function column_register_user( $column ) {
    $column['compania'] = 'compania';
    $column['cif'] = 'cif';
    return $column;
}
add_filter( 'manage_users_columns', 'column_register_user' );


// LLena las columnas con los campos extra
function column_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'compania' :
            return get_user_meta( 'compania', $user_id );
            break;
        case 'cif' :
            return get_user_meta( 'cif', $user_id );
            break;
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'column_user_table_row', 10, 3 );
    
answered by 26.03.2017 в 11:36