Home > Software engineering >  Display User Meta in Users.php column
Display User Meta in Users.php column

Time:11-12

In WordPress user's profile I have custom user meta setup _yith_wcact_user_ban

The custom field is a true / false checkbox.

I have added the necessary code to functions.php that adds this custom user meta to display in columns of users.php.

However, how do you return the value to display Verfied or Unauthorised in the column?

_yith_wcact_user_ban = true = Unauthorised

_yith_wcact_user_ban = false = Verfied

function add_custom_column_name($columns) {
    $columns['columns_array_name'] = 'Verification';
    return $columns;
}
function show_custom_column_values($value, $column_name, $user_id) {
    if ( 'columns_array_name' == $column_name )
        return get_user_meta( $user_id, '_yith_wcact_user_ban', true );
return('Unauthorised');
}

add_filter('manage_users_columns', 'add_custom_column_name');
add_action('manage_users_custom_column', 'show_custom_column_values', 10, 3);

Any help would be much appreciated.

Thank you.

enter image description here

CodePudding user response:

manage_users_custom_column is a filter hook, not an action hook. try below code.

function add_custom_column_name($columns) {
    $columns['verification'] = 'Verification';
    return $columns;
}
function show_custom_column_values($value, $column_name, $user_id) {
    if ( 'verification' == $column_name ){
        if( get_user_meta( $user_id, '_yith_wcact_user_ban', true ) ){
            return "Unauthorised";
        }else{
            return "Verfied";
        }
    }
    return $value;
}

add_filter('manage_users_columns', 'add_custom_column_name');
add_filter('manage_users_custom_column', 'show_custom_column_values', 10, 3);

Tested and works.

enter image description here

  • Related