在 WordPress 的管理员用户列表自定义列中显示用户元数据值 [英] Display user meta data values in admin user list custom columns in WordPress

查看:63
本文介绍了在 WordPress 的管理员用户列表自定义列中显示用户元数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Woocommerce,在

在数据库中,有一些名为billing_vatnrbilling_companymeta_key 值来自WooCommerce 注册表并保存在wp_usermeta 中 表.

我想弄清楚的是如何为这些元键显示相应的 meta_value 并为每个用户在各自的列中显示它们.

换句话说,在VAT Nr字段中,应该显示元键billing_vatnr的内容,如果没有内容,则显示N/一个.带有 billing_company 的 Company Name 列相同.

这是我迄今为止尝试过的:

add_filter('manage_users_custom_column', 'vatnr_status_data', 10, 3);函数 vatnr_status_data( $value, $column_name, $user_id ) {如果('account_vatnr' == $column_name){if( $billing_vatnr = get_user_meta( $user_id, 'billing_vatnr', true )) {回声 $billing_vatnr;} else { 回声不适用";}}返回 $value;}

但它不起作用.

以下是我添加的不同列:

//创建列add_action('manage_users_columns','account_verification_status_and_company_columns');函数 account_verification_status_and_company_columns($column_headers) {取消设置($column_headers['posts']);$column_headers['account_verification'] = __('验证状态');$column_headers['account_vatnr'] = __('VAT Nr');$column_headers['account_companyname'] = __('公司名称');返回 $column_headers;}//获取验证状态,感谢 LoicTheAztecadd_filter('manage_users_custom_column', 'user_account_verification_status_data', 10, 3);函数 user_account_verification_status_data( $value, $column_name, $user_id ) {如果('account_verification' == $column_name){if( get_user_meta( $user_id, 'is_activated', true ) == 1 ) {$value = '<span style="color:green;font-weight:bold;">已验证</span>';} 别的 {$value = '<span class="na" style="color:grey;"><em>未验证</em></span>';}}返回 $value;}

非常感谢任何帮助.

解决方案

尝试以下稍微重新访问的代码,其中为 account_vatnraccount_companyname 附加自定义字段添加了一些内容:

//将自定义列添加到管理员用户列表add_action('manage_users_columns', 'add_custom_users_columns', 10, 1 );函数 add_custom_users_columns( $columns ) {未设置($columns['posts']);$columns['account_verification'] = __('验证状态');$columns['account_vatnr'] = __('VAT Nr');$columns['account_companyname'] = __('公司名称');返回 $columns;}//获取验证状态,感谢 LoicTheAztecadd_filter('manage_users_custom_column', 'add_data_to_custom_users_columns', 10, 3);函数 add_data_to_custom_users_columns( $value, $column_name, $user_id ) {如果('account_verification' == $column_name){if( get_user_meta( $user_id, 'is_activated', true ) == 1 ) {$value = '<span style="color:green;font-weight:bold;">已验证</span>';} 别的 {$value = '

代码位于您的活动子主题(活动主题)的functions.php 文件中.经测试有效.


wp_usermeta数据库表中的这个注册数据:

您将在自定义列的管理员用户列表中看到以下显示:

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.

解决方案

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:

这篇关于在 WordPress 的管理员用户列表自定义列中显示用户元数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆