# First and Last Name on the WooCommerce Registration Form

> Adds required first and last name fields to the My Account registration form and saves them to the customer's profile and billing details.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, registration, my-account, fields
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-registration-name-fields/

## Code

```php
add_action( 'woocommerce_register_form_start', function () {
    // phpcs:disable WordPress.Security.NonceVerification -- Repopulating the form after a failed submit.
    $first = isset( $_POST['billing_first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_first_name'] ) ) : '';
    $last  = isset( $_POST['billing_last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_last_name'] ) ) : '';
    // phpcs:enable
    ?>
    <p class="form-row form-row-first">
        <label for="reg_billing_first_name"><?php echo esc_html( 'First name' ); ?> <span class="required" aria-hidden="true">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" autocomplete="given-name" required value="<?php echo esc_attr( $first ); ?>">
    </p>
    <p class="form-row form-row-last">
        <label for="reg_billing_last_name"><?php echo esc_html( 'Last name' ); ?> <span class="required" aria-hidden="true">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" autocomplete="family-name" required value="<?php echo esc_attr( $last ); ?>">
    </p>
    <div class="clear"></div>
    <?php
} );

add_filter( 'woocommerce_registration_errors', function ( $errors ) {
    // phpcs:disable WordPress.Security.NonceVerification -- WooCommerce verifies the registration nonce.
    if ( empty( $_POST['billing_first_name'] ) || '' === trim( sanitize_text_field( wp_unslash( $_POST['billing_first_name'] ) ) ) ) {
        $errors->add( 'billing_first_name_error', 'Please enter your first name.' );
    }

    if ( empty( $_POST['billing_last_name'] ) || '' === trim( sanitize_text_field( wp_unslash( $_POST['billing_last_name'] ) ) ) ) {
        $errors->add( 'billing_last_name_error', 'Please enter your last name.' );
    }
    // phpcs:enable

    return $errors;
} );

add_action( 'woocommerce_created_customer', function ( $customer_id ) {
    // phpcs:disable WordPress.Security.NonceVerification -- WooCommerce verifies the registration nonce.
    foreach ( array( 'first_name', 'last_name' ) as $field ) {
        if ( isset( $_POST[ 'billing_' . $field ] ) ) {
            $value = sanitize_text_field( wp_unslash( $_POST[ 'billing_' . $field ] ) );
            update_user_meta( $customer_id, $field, $value );
            update_user_meta( $customer_id, 'billing_' . $field, $value );
        }
    }
    // phpcs:enable
} );
```

## Notes

Enable "Allow customers to create an account on the My account page" in WooCommerce > Settings > Accounts to see the form.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "First and Last Name on the WooCommerce Registration Form" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
