skip to Main Content

I have added a custom meta field to users called "customer_code". This all works and stores the information just fine.

I have added the "Customer Code" column to the users admin page like so:

add_filter('manage_users_columns', 'db_add_customer_code_column');
function db_add_customer_code_column($columns) {
    $columns = array_merge(array('customer_code' => 'Customer Code'), $columns);
    return $columns;
}

Again, this works just fine and the column appears on the users admin page.

However, I then have this code to populate the column with the "customer_code" meta data:

add_action('manage_users_custom_column',  'db_show_customer_code_column_content', 10, 3);
function db_show_customer_code_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ('customer_code' == $column_name){
        $customer_code = get_the_author_meta( 'customer_code', $user_id );
        return $customer_code;
        return $value;
    }
}

The column, however, remains completely blank!

I have used this exact same code on another site and it works perfectly there. It’s also definitely getting the customer code too because if I use "echo $customer_code" (instead of "return") then it correctly spits out all the customer codes across the top of the page; it just seems to be refusing to return each of them into the column for some reason!

Any ideas???

2

Answers


  1. Chosen as BEST ANSWER

    It looks like there was a conflict because something else (perhaps in the theme) is also calling the "manage_users_custom_column" filter.

    Increasing my call's priority has fixed the issue (changed "10" to "15"):

    add_filter('manage_users_custom_column',  'db_show_customer_code_column_content', 15, 3);
    

  2. There are some mistakes in your code. From the available user ID, you should better use WordPress get_user_meta function than get_the_author_meta function, to get and display custom user metadata.

    Also note that manage_users_custom_column is a filter hook, that requires to return the main argument $output at the end, inside the related hooked function.

    Try the following instead:

    add_filter('manage_users_columns', 'db_add_customer_code_column');
    function db_add_customer_code_column($columns) {
        return array_merge( array('customer_code' => esc_html__('Customer Code') ), $columns );
    }
    
    
    add_filter('manage_users_custom_column',  'db_show_customer_code_column_content', 10, 3);
    function db_show_customer_code_column_content( $output, $column_name, $user_id) {
        if ( 'customer_code' === $column_name ){
            $output = get_user_meta( $user_id, 'customer_code', true );
        }
        return $output;
    }
    

    It should work.

    If I replace:

    $output = get_user_meta( $user_id, 'customer_code', true );
    

    with:

    $output = get_user_meta( $user_id, 'first_name', true );
    

    I get something like:

    admin users list custom column

    So as you can see the column is populated, with the user’s first name. Be sure that customer_code is the right meta_key to be used, checking in wp_usermeta database table.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search