简体   繁体   English

将一些我的帐户自定义字段添加到 Woocommerce 的管理员用户页面

[英]Adding some my account custom fields to admin user pages in Woocommerce

In another question on here a user asked How to add a custom fields in Edit Account page在此处的另一个问题中,用户询问如何在“编辑帐户”页面中添加自定义字段

I am looking to do the same thing but also to add the same custom field on the users profile page where its visible to an Administrator.我希望做同样的事情,但也在用户配置文件页面上添加相同的自定义字段,管理员可以看到它。 Is this possible?这可能吗?

Updated - To add those two "Favorite color" custom fields in Wordpress admin user pages, you will use the following:更新- 要在 Wordpress 管理用户页面中添加这两个“最喜欢的颜色”自定义字段,您将使用以下内容:

// Add the custom field "favorite_color" in admin user
add_action( 'show_user_profile', 'add_extra_custom_user_data', 1, 1 );
add_action( 'edit_user_profile', 'add_extra_custom_user_data', 1, 1 );
function add_extra_custom_user_data( $user )
{
    ?>
        <h3><?php _e("Other details",'woocommerce' ); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?></label></th>
                <td><input type="text" name="favorite_color" value="<?php echo esc_attr(get_the_author_meta( 'favorite_color', $user->ID )); ?>" class="regular-text" /></td>
            </tr><tr>
                <th><label for="favorite_color2"><?php _e( 'Favorite color 2', 'woocommerce' ); ?></label></th>
                <td><input type="text" name="favorite_color2" value="<?php echo esc_attr(get_the_author_meta( 'favorite_color2', $user->ID )); ?>" class="regular-text" /></td>
            </tr>
        </table>
        <br />
    <?php
}

// Save the custom field 'favorite_color' in admin user
add_action( 'personal_options_update', 'save_extra_custom_user_data' );
add_action( 'edit_user_profile_update', 'save_extra_custom_user_data' );
function save_extra_custom_user_data( $user_id )
{
    if( ! empty($_POST['favorite_color']) )
        update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );

    if( ! empty($_POST['favorite_color2']) )
        update_user_meta( $user_id, 'favorite_color2', sanitize_text_field( $_POST['favorite_color2'] ) );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:将自定义模板添加到客户帐户页面 - WooCommerce: Adding custom template to customer account pages 具有 WooCommerce 管理订单页面的多个自定义字段的 Metabox - Metabox with multiple custom fields for WooCommerce admin order pages 查看/更新管理 WP 用户页面中的 WooCommerce 用户自定义字段 - View/update WooCommerce user custom field in admin WP user pages 将自定义 WooCommerce 注册字段添加到管理 WordPress 用户联系人字段 - Add custom WooCommerce registration fields to admin WordPress user contact fields 在 WooCommerce 管理新订单自定义字段上加载用户自定义数据 - Load user custom data on WooCommerce admin new order custom fields WooCommerce:在我的帐户页面中为自定义模板分配端点 - WooCommerce: Assigning an endpoint to a custom template in my account pages WooCommerce -Thankyou 和“我的帐户”查看订单页面上的自定义通知 - WooCommerce - Custom notice on Thankyou and "My account" view order pages WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's edit page Woocommerce:验证我的帐户编辑页面上的自定义字段 - Woocommerce: validate custom fields on my account edit page 在[woocommerce_my_account]的左侧添加自定义链接 - Adding a custom link to left of [woocommerce_my_account]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM