对于 Woocommerce,在 this answer thread 的帮助下,我在后端(管理员)用户列表中创建了一些自定义列:
在数据库中,有一些meta_key
调用的值 billing_vatnr
和 billing_company
来自 WooCommerce 注册表并保存在 wp_usermeta
table 。
我想弄清楚的是如何显示相应的 meta_value
对于这些元键,并为每个用户在各自的列中显示它们。
换句话说,在增值税号 字段,元键的内容billing_vatnr
应该显示,如果没有内容,显示N/A
.公司名称列与 billing_company
相同.
到目前为止,这是我尝试过的:
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;
}
但它不起作用。
以下是我添加的不同列:
// 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;
}
很感谢任何形式的帮助。
最佳答案
尝试使用以下稍微重新访问的代码,并为 account_vatnr
添加一些内容和 account_companyname
其他自定义字段:
// 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;
}
代码位于您的事件子主题(事件主题)的 functions.php 文件中。测试和工作。来自
wp_usermeta
中的这个注册数据数据库表:您将在自定义列的管理员用户列表中获得以下显示:
关于php - 在 WordPress 的管理员用户列表自定义列中显示用户元数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52690031/