Use custom user fields in the user list in WordPress
I have installed a plugin in wordpress that allows inserting registration forms for users with custom fields (in my case, "Company" and "CIF")
Data insertion is successful, but I also want to display it in the users section as in the screenshot I attach:
For this, I am using the following functions that I add to the functions.php
file:
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 he 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 I have entered in the form.
The problem I have is that when trying the same thing for CIF I stop showing the company data. I can only display one data type at a time and I don't know why it can be.
2 answers
The code you use to create the CIF column is missing. Assuming it's an exact copy of the one you use for the company column, I think the problem is that you're adding the action manage_users_custom_column
twice, whereby the second "over-writes" 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 haven't used WordPress for a long time and could be totally wrong:)
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 );