Admin 用户列表自定义列显示基于 Wordpress 中的自定义用户元数据

Admin user list custom column display based on custom user metadata in Wordpress

我为 WordPress 用户创建了一个名为 "Verification" 的自定义栏。在 WooCommerce 注册后,会向用户发送一封电子邮件,要求他们验证他们的电子邮件和帐户。此信息作为 meta_key 存储在 usermeta (wp_usermeta) 下的数据库中,该密钥称为 is_activated.

自定义列的代码如下:

add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
  unset($column_headers['posts']);
  $column_headers['account_verification'] = 'Verification Status';
  return $column_headers;
}

function make_verification_status_column_sortable( $columns ) {
    $columns['account_verification'] = 'Verification Status';
    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );

我现在正试图找出如何从用户元 table 获取数据,在本例中,是 meta_key is_activated。这是我尝试过的:

add_action('manage_users_custom_column', 'account_verification_information', 10, 3);
function account_verification_information($status = '1') {

    global $wpdb;
    $has_user_verified = $wpdb->get_var( $wpdb->prepare(
        "SELECT COUNT(*) FROM $wpdb->users
        LEFT JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
        WHERE meta_key = 'is_activated'
        AND meta_value = '$status';"
    , $level ));

    return $has_user_verified;
    $check_verification = account_verification_information(1);

  if ( $column_name === account_verification ) {
            echo $check_verification;
            } else {
                echo '<span class="na">Not yet Verified</span>';
            }
    }

希望通过查看它,您 "get the idea"。 如果值为 1,我需要它显示 "Verified";如果值为 0,我需要它显示 "Not Verified"。

我试过很多不同的版本,我都迷路了..

非常感谢任何帮助。

请尝试以下操作:

add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
    unset($column_headers['posts']);

    $column_headers['account_verification'] = __('Verification Status');

    return $column_headers;
}

add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );
function make_verification_status_column_sortable( $vars ) {
    $columns['account_verification'] = 'account_verification';

    return $columns;
}

add_filter('manage_users_custom_column',  'add_user_column_value', 10, 3);
function add_user_column_value( $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;
}

代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。