shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

How to add Custom Fields to the theme’s registration form?

The below steps will help you add custom fields to the theme’s registration form.

  1. Add the custom field to the form.
  2. Validate the input.
  3. Save the input to the user’s metadata.
  4. Show setting in User’s edit profile page in WordPress Dashboard.

Add the custom field to the form

add_action( 'elumine_after_registartion_form', 'elumine_register_add_custom_fields', 9 );

if ( ! function_exists( 'elumine_register_add_custom_fields' ) ) {
	function elumine_register_add_custom_fields() {
		?>
		<input type="text" name="my_custom_field" required="true" placeholder="<?php esc_attr_e( 'My Custom Field*', 'elumine-child' ); ?>" />
		<?php
	}
}
  • Add required=”true” only if the field is required.
Registration Form

Validate the input.

add_filter( 'elumine_before_process', 'elumine_validate_custom_fields', 9, 2 );

if ( ! function_exists( 'elumine_validate_custom_fields' ) ) {
	function elumine_validate_custom_fields( $errors, $fields ) {
		/*Add the following condition only if the field is required*/
		if ( empty( $fields['my_custom_field'] ) ) {
			$errors['my_custom_field'] = __( 'My Custom Field is mandatory.', 'elumine-child' );
		}
		// Add other validations here as per the input requirement.
		return $errors;
	}
}

Save the input to the user’s metadata.

add_action( 'register_new_user', 'elumine_save_custom_fields', 10, 1 );

function elumine_save_custom_fields( $user_id ) {
	if ( isset( $_POST['my_custom_field'] ) ) {
		update_user_meta( $user_id, 'my_custom_field', $_POST['my_custom_field'] );
	}
}

Show setting in User’s edit profile page.

// Hooks near the bottom of profile page (if current user) 
add_action('show_user_profile', 'elumine_custom_user_profile_fields', 9999);

// Hooks near the bottom of the profile page (if not current user) 
add_action('edit_user_profile', 'elumine_custom_user_profile_fields', 9999);

// @param WP_User $user
function elumine_custom_user_profile_fields( $user ) {
?>
    <table class="form-table">
        <tr>
            <th>
                <label for="my_custom_field"><?php _e( 'My Custom Field', 'elumine-child' ); ?></label>
            </th>
            <td>
                <input type="text" name="my_custom_field" id="my_custom_field" value="<?php echo esc_attr( get_the_author_meta( 'my_custom_field', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php
}


// Hook is used to save custom fields that have been added to the WordPress profile page (if current user) 
add_action( 'personal_options_update', 'elumine_update_extra_profile_fields' );

// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user) 
add_action( 'edit_user_profile_update', 'elumine_update_extra_profile_fields' );

function elumine_update_extra_profile_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) )
        update_user_meta( $user_id, 'my_custom_field', $_POST['my_custom_field'] );
}
User Edit page
Updated on July 21, 2020
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Was this article helpful?

shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Related Articles