简体   繁体   中英

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:

// 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). Tested and works.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM