Admin user list custom column display based on cus

2019-06-13 02:02发布

问题:

I have created a custom column for WordPress users called "Verification". Upon WooCommerce registration, an email is sent to the user asking them to verify their email and that way, account. This information is stored in the database under usermeta (wp_usermeta) as a meta_key and that key is called is_activated.

Here is the code for the custom column:

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' );

I am now trying to figure out how to fetch data from the usermeta table and in this case, the meta_key is_activated. Here is what I tried:

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>';
            }
    }

Hopefully, by looking at it, you "get the idea". I need it to show "Verified" if the value is 1 and "Not Verified" if the value is 0.

I've tried so many different versions of this I am getting lost..

Any help is highly appreciated.

回答1:

Try the following instead:

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;
}

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