Customized form-login.php template file in Child Theme – Add Validation and Save
-
I have a child theme that I am attempting to add additional form fields on the registration form. I followed the direction in How to add custom fields in user registration on the “My Account” page resulting in the following function:
/** * Add new register fields for WooCommerce registration. * * @return string Register fields HTML. */ // Function to check starting char of a string function startsWith($haystack, $needle){ return $needle === '' || strpos($haystack, $needle) === 0; } // Custom function to display the Billing Address form to registration page function my_custom_function(){ global $woocommerce; $checkout = $woocommerce->checkout(); foreach ($checkout->checkout_fields['billing'] as $key => $field) : if(!($key == 'billing_first_name' || $key == 'billing_last_name')){ $field['required'] = false; } if(!($key == 'billing_email')){ woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); } endforeach; ?> <br/> <?php $checked = $checkout->get_value( 'newsletter_opt_in1' ) ? $checkout->get_value( 'newsletter_opt_in1' ) : 1; woocommerce_form_field("newsletter_opt_in1", array( 'type' => 'checkbox', 'class' => array('form-row-wide input-checkbox'), 'label' => 'I would like to recieve the newsletter', 'description' => '', 'default' => '' ), $checked ); } add_action('woocommerce_register_form','my_custom_function'); // Custom function to save Usermeta or Billing Address of registered user function save_address($user_id){ global $woocommerce; $address = $_POST; foreach ($address as $key => $field) : if(startsWith($key,'billing_')){ // Condition to add firstname and last name to user meta table if($key == 'billing_first_name' || $key == 'billing_last_name'){ $new_key = explode('billing_',$key); update_user_meta( $user_id, $new_key[1], $_POST[$key] ); } update_user_meta( $user_id, $key, $_POST[$key] ); } endforeach; if ( isset( $_POST['newsletter_opt_in'] ) ) { update_user_meta( $user_id, 'newsletter_opt_in', $_POST['newsletter_opt_in']); } } add_action('woocommerce_created_customer','save_address'); /** * Validate the extra register fields. * * @param string $username Current username. * @param string $email Current email. * @param object $validation_errors WP_Error object. * * @return void */ function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) { if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) { $validation_errors->add( 'billing_first_name_error', __( 'First name is required!', 'woocommerce' ) ); } if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { $validation_errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'woocommerce' ) ); } } add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
The issue I have with this approach is that unless I add the above code blocks to the parent functions.php file the additional fields do no show.
Therefore, I decided to modify the form-login.php file and add it to my child theme with the following code block:
<p class="form-row form-row-first"> <label for="reg_billing_first_name" class=""><?php _e( 'First Name', 'woocommerce' ); ?><span class="required" title="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'] ) ) echo esc_attr( $_POST['billing_first_name'] ); ?>"> </p> <p class="form-row form-row-last"> <label for="reg_billing_last_name" class=""><?php _e( 'Last Name', 'woocommerce' ); ?><span class="required" title="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'] ) ) echo esc_attr( $_POST['billing_last_name'] ); ?>"> </p> <div class="clear"></div> <p class="form-row form-row-wide"> <label for="reg_billing_company" class=""><?php _e( 'Company Name', 'woocommerce' ); ?></label> <input type="text" class="input-text " name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) echo esc_attr( $_POST['billing_company'] ); ?>"> </p> <p class="form-row form-row-wide"> <label for="reg_billing_phone" class=""><?php _e( 'Phone', 'woocommerce' ); ?></label> <input type="tel" class="input-text " name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) echo esc_attr( $_POST['billing_phone'] ); ?>"> </p> <div class="clear"></div> <?php $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/'; $frontend_script_path = $assets_path . 'js/frontend/'; wp_enqueue_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js' ); wp_enqueue_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js' ); wp_enqueue_script( 'wc-password-strength-meter', $frontend_script_path . 'password-strength-meter' . $suffix . '.js', array( 'jquery', 'password-strength-meter' ) ); woocommerce_form_field("billing_country", array( 'type' => 'country', 'class' => array('form-row-wide'), 'label' => 'Country', 'description' => '', 'default' => '' ), esc_attr( $_POST['billing_country'] ) ); ?> <p class="form-row form-row-wide"> <label for="reg_billing_address_1" class=""><?php _e( 'Address', 'woocommerce' ); ?></label> <input type="text" class="input-text " name="billing_address_1" id="reg_billing_address_1" placeholder="Street address" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) echo esc_attr( $_POST['billing_address_1'] ); ?>"> </p> <p class="form-row form-row-wide"> <input type="text" class="input-text " name="billing_address_2" id="reg_billing_address_2" placeholder="Apartment, suite, unit etc. (optional)" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) echo esc_attr( $_POST['billing_address_2'] ); ?>"> </p> <p class="form-row form-row-wide"> <label for="reg_billing_city" class=""><?php _e( 'Town / City', 'woocommerce' ); ?></label> <input type="text" class="input-text " name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) echo esc_attr( $_POST['billing_city'] ); ?>"> </p> <?php woocommerce_form_field("billing_state", array( 'type' => 'state', 'class' => array('form-row-first'), 'label' => 'State / County', 'description' => '', 'default' => '' ), esc_attr( $_POST['billing_state'] ) ); ?> <p class="form-row form-row-last"> <label for="reg_billing_postcode" class=""><?php _e( 'Postcode / ZIP', 'woocommerce' ); ?></label> <input type="text" class="input-text " name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) echo esc_attr( $_POST['billing_postcode'] ); ?>"> </p> <div class="clear"></div> <br> <p class="form-row form-row-wide"> <label for="reg_newsletter_opt_in" class=""><input type="checkbox" class="input-checkbox " name="newsletter_opt_in" id="reg_newsletter_opt_in" value="1" checked="checked"><?php _e( 'I would like to recieve the newsletter', 'woocommerce' ); ?> </label> </p>
This is working for displaying the fields but now I am struggling to identify how I add the validation and save functions?
I am not set on using either solution and would appreciate guidance on how best to accomplish this in my child theme. Thank you.
- The topic ‘Customized form-login.php template file in Child Theme – Add Validation and Save’ is closed to new replies.