Default WordPress registration form has only two fields namely username and email, password for the user gets auto-generated and emailed to him/her.

There may need arises to add some more fields for registration, as we may require the user to enter some of his additional information when registering to our website. In such a case we can add custom fields to WordPress default registration form using hooks. ( After all hooks create magics in WordPress 😉 )

wordpress-registration-form

Main hooks which will be used to add custom fields to WordPress registration form are:

1. register_form: For adding custom fields.
2. registration_errors: For validation of custom field values.
3. user_register: For saving values of custom fields, if there are no errors.

Now, we will make a demo for the same. Let’s assume that we want to add “City” field to our registration form, so let’s start working on it.

Step 1: Add field to registration form

This step will be accomplished by register_form hook of WordPress.

function add_city_field(){ ?>

<label for="user_city"><!--?php _e('City') ?-->

		<input class="input" id="user_city" type="number" name="user_city" value="" /></label>

<!--?php } add_action('register_form', 'add_city_field'); 

Step 2: When user will submit the form, we have to validate the field’s value whether it has been entered properly or not ! For validation registration_errors hooks will be useful, callback function for registration_errors filter accepts 3 arguments namely: $errors: WP_Error object $sanitized_user_login: Username entered by user $user_email: Email of user If there is any error we will add that error to $error, which is WP_Error object and will return it from our function.

 function validations($errors, $sanitized_user_login, $user_email){ 	$city = empty($_POST['user_city']) ? '' : $_POST['user_city']; 	$city = trim($city); 	if( empty($city) ){ 		$errors->add('empty_city', 'Enter city');
	}elseif( !preg_match("/^[A-Za-z]*$/", $new_password) ){

		$errors->add('invalid_city_name', 'Enter valid city name');
	}

}
add_action('registration_errors', 'validations', 50,3);

Here we are performing two step check, one is to ensure that city name is not empty and other is to ensure that city name is only alphabetic. This is just an example, you can validate the fields in your own way.

Step 3: Saving the value of custom field.

For this, we will use user_register hook, this hook is fired once the user has created successfully. Callback function for this hook will receive one argument i.e. $user_id which is ID of newly created user. We will store value of custom field in usermeta table of WordPress, so that the value will be somehow associated with the user and will be easily accessible for future reference.

function save_info($userid){

	update_user_meta( $user_id, 'user_city', $_POST['user_city'] );

}
add_action('user_register', 'save_info');

In these 3 easy steps we can add custom fields to WordPress registration form.

If you have any thoughts in your mind don’t forget to publish the same in comments. 🙂

Add fields to wordpress registration form
Tagged on:         

4 thoughts on “Add fields to wordpress registration form

Leave a Reply

Your email address will not be published. Required fields are marked *