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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.