Add Name and Confirm Password Fields to WooCommerce Registration

Quick Summary
- woocommerce_register_form_start adds required First and Last name fields, woocommerce_registration_errors blocks empty ones, and woocommerce_created_customer saves them.
- The names are saved only as billing_first_name and billing_last_name, so profile and Account details names stay empty unless you also save first_name and last_name.
- A Confirm password field added on woocommerce_register_form must match the password, and it only works when the form shows WooCommerce's own password field.
By default, the registration form on WooCommerce’s My account page asks only for an email address and, depending on your settings, a username and password. This snippet adds required First name and Last name fields, saves them to the customer’s account and adds a Confirm password field that has to match the password.
Registration must be enabled on the My account page (WooCommerce > Settings > Accounts & Privacy) for the form to show. Add the code to your child theme’s functions.php or a code snippets plugin.
add_action( 'woocommerce_register_form_start', 'prefix_add_name_woo_account_registration' );
function prefix_add_name_woo_account_registration() {
?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
///////////////////////////////
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'prefix_validate_name_fields', 10, 3 );
function prefix_validate_name_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
return $errors;
}
///////////////////////////////
// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'prefix_save_name_fields' );
function prefix_save_name_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
}
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
if ( ! isset( $_POST['password'] ) ) {
return $reg_errors; // no password field: WooCommerce generates the password
}
$password = wp_unslash( $_POST['password'] );
$password2 = isset( $_POST['password2'] ) ? wp_unslash( $_POST['password2'] ) : '';
if ( strcmp( $password, $password2 ) !== 0 ) {
$reg_errors->add( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
}
return $reg_errors;
}
add_action( 'woocommerce_register_form', 'prefix_register_form_password_repeat',5 );
function prefix_register_form_password_repeat() {
?>
<p class="form-row form-row-wide">
<label for="reg_password2"><?php _e( 'Confirm Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
<?php
}First and last name fields
woocommerce_register_form_startprints both fields at the top of the form. If the form reloads with an error, the values the customer typed are filled back in.woocommerce_registration_errorsadds an error when a name field is submitted empty, which stops the account from being created.woocommerce_created_customersaves the sanitized values asbilling_first_nameandbilling_last_nameuser meta, so they prefill the billing fields at checkout.
These are billing fields only. The first and last name in the user profile and in Account details stay empty unless you also save the values to first_name and last_name.
Confirm password field
prefix_register_form_password_repeat()adds the field onwoocommerce_register_form. The priority of5places it before other content hooked to the same spot at the default priority.registration_errors_validation()readspasswordandpassword2from$_POST, compares them withstrcmp()and adds a “Passwords do not match.” error when they differ. The values are only unslashed, not run throughsanitize_text_field(), because that could change a password, and the function only compares them.- The check needs WooCommerce’s own password field. If your settings make WooCommerce generate the password or email a link to set it, the form has no password field and the check skips itself, but the Confirm password field would still show, so leave this part out.
- The error is added to the
$reg_errorsobject the filter receives, so a missing name and a password mismatch show together.
For registration and account forms built around the data your store actually needs, see my WooCommerce development services.
Related articles
- checking that passwords match in real time: instant feedback while the customer types
- fixing the tab order in sign-up forms: keyboard navigation between the new fields
- removing the Dashboard tab from My account: another My account page tweak
