I’m going to show how to add custom field to user registration form in wordpress.I checked through the internet to add this custom field and how to validate that field for values.But couldn’t find useful articles. So I decided to show simple guide to how to add custom field to user registration form and how to validate it for values from simple step to advanced steps.So special thing is you don’t have to use any plugins for this.
Step -1 : Add custom field to registration form
1-Find your current theme folder and it’s functions.php file.
2-Open that file, copy the following code and paste at the end of the page.
add_action( 'register_form', 'my_custom_add_register_question' ); if ( ! function_exists( 'my_custom_add_register_question' ) ) { function my_custom_add_register_question() { ?>
3-Save the file and go to register page and check for filed just now added.
Step -2 : Validate field for empty value
Here we need to do the validate process for empty value of we added filed.So do this copy the following function and add the end of the functions.hp file.
add_action( 'register_post', 'my_custom_field_validate', 10, 3 ); if ( ! function_exists( 'my_custom_field_validate' ) ) { function my_custom_field_validate( $sanitized_user_login, $user_email, $errors) { //check value for empty if ( (!isset( $_POST['my_custom_field'] )) || (trim($_POST['my_custom_field']) == '') ) { return $errors->add( 'my_custom_field', 'ERROR: Please enter for My Custom Field' ); } // get value if ( isset($_POST['my_custom_field']) && ($_POST['my_custom_field'] != '') ) { $my_custom_field_value = trim($_POST['my_custom_field']); } } }When you entered empty values then error message is like this.
That's only.
In the next post I'll show you how to how to add security question in to the registration form to stop spams.But it is without any plugins. 😉
If you have suggestions or ideas or problems, leave a comment bellow.