Display user meta data values in admin user list custom columns in WordPress

自古美人都是妖i 提交于 2020-07-07 01:39:48

问题


For Woocommerce, with the help of this answer thread, I created some custom columns in Back end (Admin) user list:

In the database, there are some meta_key values called billing_vatnr and billing_company coming from the WooCommerce registration form and saved in wp_usermeta table.

What I'm trying to figure out is how to display the corresponding meta_value for those meta keys and show them in their respective column for each user.

In other words, in the VAT Nr field, the content of the meta key billing_vatnr should be displayed and if there is no content, display N/A. Same for Company Name column with billing_company.

This is what I've tried so far is:

add_filter('manage_users_custom_column',  'vatnr_status_data', 10, 3);
function vatnr_status_data( $value, $column_name, $user_id ) {
    if ( 'account_vatnr' == $column_name ) {
        if( $billing_vatnr = get_user_meta( $user_id, 'billing_vatnr', true )) {
            echo $billing_vatnr; } else { echo "N/A"; }
    }
    return $value;
}

But it doesn't work.

Here are the different columns I've added:

// creating the columns
add_action('manage_users_columns','account_verification_status_and_company_columns');
function account_verification_status_and_company_columns($column_headers) {
    unset($column_headers['posts']);
    $column_headers['account_verification'] = __('Verification Status');
    $column_headers['account_vatnr'] = __('VAT Nr');
    $column_headers['account_companyname'] = __('Company Name');
    return $column_headers;
}


// fetching the verification status, thanks to LoicTheAztec
add_filter('manage_users_custom_column',  'user_account_verification_status_data', 10, 3);
function user_account_verification_status_data( $value, $column_name, $user_id ) {
    if ( 'account_verification' == $column_name ) {
        if( get_user_meta( $user_id, 'is_activated', true ) == 1 ) {
            $value = '<span style="color:green;font-weight:bold;">Verified</span>';
        } else {
            $value = '<span class="na" style="color:grey;"><em>Not Verified</em></span>';
        }
    }
    return $value;
}

Any help is very much appreciated.


回答1:


Try the following lightly revisited code with some additions for account_vatnr and account_companyname additional custom fields:

// Add custom columns to Admin users list
add_action('manage_users_columns', 'add_custom_users_columns', 10, 1 );
function add_custom_users_columns( $columns ) {
    unset($columns['posts']);

    $columns['account_verification'] = __('Verification Status');
    $columns['account_vatnr'] = __('VAT Nr');
    $columns['account_companyname'] = __('Company Name');

    return $columns;
}


// fetching the verification status, thanks to LoicTheAztec
add_filter('manage_users_custom_column',  'add_data_to_custom_users_columns', 10, 3);
function add_data_to_custom_users_columns( $value, $column_name, $user_id ) {
    if ( 'account_verification' == $column_name ) {
        if( get_user_meta( $user_id, 'is_activated', true ) == 1 ) {
            $value = '<span style="color:green;font-weight:bold;">Verified</span>';
        } else {
            $value = '<span class="na" style="color:grey;"><em>Not Verified</em></span>';
        }
    } elseif( 'account_vatnr' == $column_name ) {
        if( $vat_nr = get_user_meta( $user_id, 'account_vatnr', true ) ) {
            $value = '<span style="color:green;font-weight:bold;">' . $vat_nr . '</span>';
        } else {
            $value = '<span class="na" style="color:grey;"><em>N/a</em></span>';
        }
    } elseif( 'account_companyname' == $column_name ) {
        if( $company = get_user_meta( $user_id, 'account_companyname', true ) ) {
            $value = '<span style="color:green;font-weight:bold;">' . $company . '</span>';
        } else {
            $value = '<span class="na" style="color:grey;"><em>N/a</em></span>';
        }

    }
    return $value;
}

Code goes in functions.php file of your active child theme (active theme). Tested and works.


From this registered data in wp_usermeta database table:

You will get the following display like in Admin users list for your custom columns:



来源:https://stackoverflow.com/questions/52690031/display-user-meta-data-values-in-admin-user-list-custom-columns-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!