Skip Navigation

[Resolved] Add admin column with custom user field

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by christopheV-2 4 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1418111

Hello,

I would like to add a admin column.
My code doesn't work.

//Add custom column
add_filter('manage_users_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['votre-sport'] = 'Sport';
return $defaults;
}
//Add rows data
add_action('manage_users_custom_column', 'my_custom_column', 10, 3 );
function my_custom_column($column, $user_id ){
switch ( $column ) {
case 'Votre sport':
echo get_user_meta($user_id, 'wpcf-votre-sport', true );
break;
}
}

Thanks

#1418169

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-12-20 at 12.11.21.png

You mean add a column for a custom field on the backend page that lists posts of some type?

Do you know you can do that in the settings for the custom post type?

See screenshot for an example from my local site.

#1418241

Hello,

No it's for Users List. It's not a post type.

Thanks

#1418419

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There are problems with your code.

The names of the columns don't match up between the two functions you have added, manage_users_custom_column is a filter not an action, and also you are using the parameters of manage_users_custom_column wrongly.

Here is a very quick version I tested on my local site for a "Team" custom user field, which works, if you want to modify your code to do something similar:

//Add custom column
add_filter('manage_users_columns', 'my_columns_head');
function my_columns_head($defaults)
{
    $defaults['team'] = 'Team';
    return $defaults;
}
//Add rows data
add_action('manage_users_custom_column', 'my_custom_column', 10, 3);
function my_custom_column($val, $column, $user_id)
{
    switch ($column) {
        case 'team':
            $val = get_user_meta($user_id, 'wpcf-team', true);
            break;
    }
    return $val;
}   
#1418431

It's perfect.

Thanks