php - 在 WordPress 的管理员用户列表自定义列中显示用户元数据值

标签 php wordpress woocommerce metadata user-data

对于 Woocommerce,在 this answer thread 的帮助下,我在后端(管理员)用户列表中创建了一些自定义列:

enter image description here

在数据库中,有一些meta_key调用的值 billing_vatnrbilling_company来自 WooCommerce 注册表并保存在 wp_usermeta table 。

enter image description here

我想弄清楚的是如何显示相应的 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 中的这个注册数据数据库表:
enter image description here
您将在自定义列的管理员用户列表中获得以下显示:
enter image description here

关于php - 在 WordPress 的管理员用户列表自定义列中显示用户元数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52690031/

相关文章:

css - 布局根据小部件中的帖子数量而变化

wordpress - WooCommerce 类别特定的添加到购物车按钮文本

php - 根据 WooCommerce 中的特定订单备注完成订单

php - 显示前导项,然后按字母顺序显示附加项

php - 需要帮助修复将生成 radio 组的类

php - 数组值中是否可以有 EOT?

php - 带 CKEditor 的文件管理器

php - 从所选变体向 WooCommerce 变量产品标题添加一些属性值

php - 如何从同一列的 WordPress 自定义字段数据库中检索总和

php - 如何获取变体的库存数量 woocommerce