Halloween Sale 15% off Ends in See the plans
PHP

Phone Number on the Account Details Form

Adds the billing phone number to the My Account "Account details" form, so customers can update it in the same place as their name and email.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'woocommerce_edit_account_form', function () {
    $phone = get_user_meta( get_current_user_id(), 'billing_phone', true );
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="account_phone"><?php echo esc_html( 'Phone number' ); ?></label>
        <input type="tel" class="woocommerce-Input input-text" name="account_phone" id="account_phone" autocomplete="tel" value="<?php echo esc_attr( $phone ); ?>">
    </p>
    <?php
} );

add_action( 'woocommerce_save_account_details_errors', function ( $errors ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the account form nonce.
    $phone = isset( $_POST['account_phone'] ) ? wc_sanitize_phone_number( wp_unslash( $_POST['account_phone'] ) ) : '';

    if ( '' !== $phone && ! WC_Validation::is_phone( $phone ) ) {
        $errors->add( 'account_phone', 'Please enter a valid phone number.' );
    }
} );

add_action( 'woocommerce_save_account_details', function ( $user_id ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the account form nonce.
    if ( isset( $_POST['account_phone'] ) ) {
        update_user_meta( $user_id, 'billing_phone', wc_sanitize_phone_number( wp_unslash( $_POST['account_phone'] ) ) );
    }
} );

Notes

Saves to the same billing phone used at checkout, so both stay in sync.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.