在WordPress的Users Admin页面中添加自定义User-Meta


Add Custom User-Meta to the Users Admin page in WordPress

我在我的站点中创建了一个特殊的表单,允许我的用户输入密钥。我使用add_user_meta()将元数据添加到数据库中。我希望能够看到这个关键,当我点击用户在管理中心。

我该如何添加到这列中?

下面是我使用

的元数据信息
add_user_meta($userId,'code','12345');

我们只希望能够将它添加到users.php的视图中,在表中显示用户名、电子邮件和角色。

我已经使用了这样的代码来显示用户id,但我不知道如何显示他们的元数据。

add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['user_id'] = 'User ID';
    return $columns;
}
add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'user_id' == $column_name )
        return $user_id;
    return $value;
}

这个例子是在WordPress codex中的这两个页面的帮助下创建的。

https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profilehttps://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update

显示和更新自定义用户元数据。

<?php
// Hooks near the bottom of profile page (if current user) 
add_action('show_user_profile', 'custom_user_profile_fields');
// Hooks near the bottom of the profile page (if not current user) 
add_action('edit_user_profile', 'custom_user_profile_fields');
// @param WP_User $user
function custom_user_profile_fields( $user ) {
?>
    <table class="form-table">
        <tr>
            <th>
                <label for="code"><?php _e( 'Custom Meta' ); ?></label>
            </th>
            <td>
                <input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'code', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php
}

// Hook is used to save custom fields that have been added to the WordPress profile page (if current user) 
add_action( 'personal_options_update', 'update_extra_profile_fields' );
// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user) 
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
function update_extra_profile_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) )
        update_user_meta( $user_id, 'code', $_POST['code'] );
}
?>

Mordred的答案在将第二个add_filter更改为add_action后为我工作。下面是修改后的代码:

function yoursite_manage_users_columns( $columns ) {
    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Subscription';
    return $columns;
}
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
    switch ( $column_key ) {
        case 'custom_field':
            $value = get_user_meta( $user_id, 'custom_field', true );
            return $value;
            break;
        default: break;
    }
    // if no column slug found, return default output value
    return $output;
}
add_action( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );

要在users.php中添加自定义user_meta字段,您需要执行以下操作:

function yoursite_manage_users_columns( $columns ) {
    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Subscription';
    return $columns;
}
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
    switch ( $column_key ) {
        case 'custom_field':
            $value = get_user_meta( $user_id, 'custom_field', true );
            return $value;
            break;
        default: break;
    }
    // if no column slug found, return default output value
    return $output;
}
add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );

意识到这是一个有点旧的线程,但是我被困在一个非常类似的问题,并认为我将分享我发现的被证明是一个非常简单的解决方案。

<?php
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
    $columns['user_id'] = 'User ID';
    return $columns;
}    
add_action('manage_users_custom_column',  'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'user_id' == $column_name )
        return $user_id;
    return $value;
}
?>

信用:https://pippinsplugins.com/add-user-id-column-to-the-wordpress-users-table/

要使额外的woocommerce字段可编辑,您可以使用以下过滤器(在本例中,在账单部分添加了自定义字段)。

add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
    function add_woocommerce_customer_meta_fields($fields)
    {
      if (isset($fields['billing']['fields'])) {
        $fields['billing']['fields']['your_custom_meta'] = array(
          'label' => __('Friendly name', 'woocommerce'),
          'description' => ''
        );
      }
      return $fields;
    }