Created
August 5, 2025 21:13
-
-
Save brinyemp4/666c8d770fa0e7c9f7d889d265f43414 to your computer and use it in GitHub Desktop.
Custom registration & profile fields in WordPress without plugin [Only theme template code]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Shown only one field example, add more fields as needed | |
| /** | |
| * If you have WooCommerce plugin installed then use the following to display fields on the registration form | |
| * If not then check actions/hooks/filters used on your registration page | |
| * To display registration form fields | |
| */ | |
| function iws_extra_register_fields() { | |
| ?> | |
| <p class="form-row form-row-first"> | |
| <label for="reg_billing_company_name" class="font-weight-medium mb-1"><?php _e('Company name', 'theme-name'); ?> <span class="required" aria-hidden="true">*</span><span class="screen-reader-text">Required</span></label> | |
| <input type="text" class="input-text" name="billing_company" id="reg_billing_company_name" value="<?php if (!empty($_POST['billing_company'])) esc_attr_e($_POST['billing_company']); ?>" required /> | |
| </p> | |
| <?php | |
| } | |
| add_action('woocommerce_register_form_start', 'iws_extra_register_fields'); | |
| /** | |
| * Use following if you have Woocommerce installed | |
| * Otherwise use registration_errors filter shown below | |
| * To validate registration form fields | |
| */ | |
| function iws_validate_extra_register_fields($username, $email, $validation_errors) { | |
| if (isset($_POST['billing_company']) && empty($_POST['billing_company'])) { | |
| $validation_errors->add('billing_company_error', __('Company name is required!', 'theme-name')); | |
| } | |
| return $validation_errors; | |
| } | |
| add_action('woocommerce_register_post', 'iws_validate_extra_register_fields', 10, 3); | |
| /** | |
| * Use the following if you don't have woocommerce | |
| * To validate registration form fields | |
| */ | |
| function iws_learndash_additional_form_fields_validation($validation_errors, $sanitized_user_login, $user_email) { | |
| if (isset($_POST['billing_company']) && empty($_POST['billing_company'])) { | |
| $validation_errors->add('billing_company_error', __('Company name is required!', 'theme-name')); | |
| } | |
| return $validation_errors; | |
| } | |
| add_filter( 'registration_errors', 'iws_learndash_additional_form_fields_validation', 10, 3 ); | |
| /** | |
| * Use following if you have woocommerce installed | |
| * Otherwise use user_register action shown below | |
| * To save registration form field values | |
| */ | |
| function iws_save_extra_register_fields($customer_id) { | |
| if (isset($_POST['billing_company'])) { | |
| update_user_meta($customer_id, 'billing_company', sanitize_text_field($_POST['billing_company'])); | |
| } | |
| } | |
| add_action('woocommerce_created_customer', 'iws_save_extra_register_fields'); | |
| /** | |
| * Use the following if you don't have woocommerce | |
| * To save registration form field values | |
| */ | |
| function iws_save_user_registration_meta($user_id) { | |
| if (isset($_POST['billing_company'])) { | |
| update_user_meta($user_id, 'billing_company', sanitize_text_field( $_POST['billing_company'])); | |
| } | |
| } | |
| add_action('user_register', 'iws_save_user_registration_meta'); | |
| /** | |
| * Display extra fields on admin edit user profile. | |
| */ | |
| function iws_extra_user_profile_fields($user) | |
| { | |
| ?> | |
| <h3><?php _e("Account Section", "blank"); ?></h3> | |
| <table class="form-table"> | |
| <tr> | |
| <th><label for="reg_billing_company"><?php _e("Company Name"); ?></label></th> | |
| <td> | |
| <input type="text" name="billing_company" id="reg_billing_company" class="regular-text" value="<?php echo esc_attr(get_the_author_meta('billing_company', $user->ID)); ?>" /> | |
| </td> | |
| </tr> | |
| <?php | |
| } | |
| add_action('show_user_profile', 'iws_extra_user_profile_fields'); | |
| add_action('edit_user_profile', 'iws_extra_user_profile_fields'); | |
| /** | |
| * Save extra fields when admin updates user information. | |
| */ | |
| function iws_save_extra_user_profile_fields($user_id) | |
| { | |
| if (current_user_can('edit_user', $user_id)) { | |
| update_user_meta($user_id, 'billing_company', $_POST['billing_company']); | |
| } | |
| return true; | |
| } | |
| add_action('personal_options_update', 'iws_save_extra_user_profile_fields'); | |
| add_action('edit_user_profile_update', 'iws_save_extra_user_profile_fields'); | |
| ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment