簡體   English   中英

將一些我的帳戶自定義字段添加到 Woocommerce 的管理員用戶頁面

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

在此處的另一個問題中,用戶詢問如何在“編輯帳戶”頁面中添加自定義字段

我希望做同樣的事情,但也在用戶配置文件頁面上添加相同的自定義字段,管理員可以看到它。 這可能嗎?

更新- 要在 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'] ) );
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM