Add a Field to WooCommerce Registration Form

In this tutorial we will add an input field into the registration form and configure a custom validation for it. Without plugins or changing WooCommerce templates 🙌

By default WooCommerce registration form contains the only field which is an email address field but what if we need to add something else there?

WooCommerce registration form
The form may also have privacy policy text which can be configured in Settings > Accounts and Privacy

FYI the registration form template is templates/form-login.php, but we are not going to use it because there two action hooks which allows us to add our field:

By the way there is also woocommerce_register_form_end which allows to add something after the button but we do not need it in this tutorial, do we?

The example below is very similar to what we did in the previous tutorial about fields in WooCommerce Edit Account page.

add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
 
function misha_add_register_form_field(){
 
	woocommerce_form_field(
		'country_to_visit',
		array(
			'type'        => 'text',
			'required'    => true, // just adds an "*"
			'label'       => 'Country you want to visit the most'
		),
		( isset($_POST['country_to_visit']) ? $_POST['country_to_visit'] : '' )
	);
 
}

Thanks again to woocommerce_form_field() function which allows to add fields of any type, not only input text fields. And we do not have to think about the field CSS styles.

Add a custom field into a WooCommerce register form

Now let’s figure it out with validation:

add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
 
function misha_validate_fields( $username, $email, $errors ) {
 
	if ( empty( $_POST['country_to_visit'] ) ) {
		$errors->add( 'country_to_visit_error', 'We really want to know!' );
	}
 
}

It is possible not only to check if the field is not empty but also to add some custom validations like “Field is too short” or “Must contain only numbers”.

How to validate a custom field added to WooCommerce registration form

And finally save the field data into the database.

add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
 
function misha_save_register_fields( $customer_id ){
 
	if ( isset( $_POST['country_to_visit'] ) ) {
		update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
	}
 
}

wc_clean() is kind of WooCommerce version of sanitize_text_field(), you can use any of them.

I recommend you to continue to the tutorial where we add the same field “Country you want to visit” into WooCommerce Edit Account page.

Learn also about…

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code