Last updated on September 24, 2023

Add a Custom Field to the User Registration Form

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Gather data with custom user fields.

To add a custom field to the user registration form in WordPress, you can use the register_form action hook along with the add_action() function. This allows you to add your own custom HTML input field to the registration form.

Here’s an example code snippet that adds a custom field called “Phone Number” to the user registration form:

function wpsnippets_add_custom_field_to_registration_form() {
    ?>
    <p>
        <label for="phone_number"><?php esc_html_e( 'Phone Number', 'text-domain' ); ?><br />
            <input type="text" name="phone_number" id="phone_number" class="input" value="<?php echo ( ! empty( $_POST['phone_number'] ) ) ? esc_attr( $_POST['phone_number'] ) : ''; ?>" />
        </label>
    </p>
    <?php
}
add_action( 'register_form', 'wpsnippets_add_custom_field_to_registration_form' );

In this example, we define a function wpsnippets_add_custom_field_to_registration_form() that outputs the HTML markup for the custom field. The esc_html_e() function is used to safely output the label text, while the esc_attr() function is used to sanitize and output the field value if it exists in the $_POST array.

You can customize the HTML markup and field name as per your requirements. Remember to replace 'text-domain' with your theme or plugin’s text domain for localization.

Once you’ve added this code to your theme’s functions.php file or a custom plugin, the custom field will be displayed on the user registration form. The value entered by the user will be saved as user meta data and can be accessed using the get_user_meta() function.

Examples

Example 1: Adding a custom field to the user registration form

This use case demonstrates how to add a custom field to the user registration form in WordPress. The code example below adds a custom field called “Phone Number” to the registration form.

function wpsnippets_add_custom_field_to_registration_form() {
    ?>
    <p>
        <label for="phone_number"><?php _e( 'Phone Number', 'textdomain' ); ?><br />
        <input type="text" name="phone_number" id="phone_number" class="input" value="<?php echo esc_attr( $_POST['phone_number'] ); ?>" size="25" /></label>
    </p>
    <?php
}
add_action( 'register_form', 'wpsnippets_add_custom_field_to_registration_form' );

The code above adds a new input field to the user registration form with the label “Phone Number”. The value entered in this field will be stored in the phone_number user meta field.

Example 2: Validating and saving the custom field value

This use case demonstrates how to validate and save the value of the custom field added to the user registration form. The code example below validates the phone number field and saves it to the user meta.

function wpsnippets_validate_custom_field( $errors, $sanitized_user_login, $user_email ) {
    if ( empty( $_POST['phone_number'] ) ) {
        $errors->add( 'phone_number_error', __( '<strong>ERROR</strong>: Please enter your phone number.', 'textdomain' ) );
    } else {
        update_user_meta( $user_id, 'phone_number', sanitize_text_field( $_POST['phone_number'] ) );
    }
    return $errors;
}
add_filter( 'registration_errors', 'wpsnippets_validate_custom_field', 10, 3 );

The code above checks if the phone number field is empty. If it is empty, an error message is displayed. If the field is not empty, the value is saved to the phone_number user meta field using the update_user_meta() function.

Example 3: Displaying the custom field value in user profile

This use case demonstrates how to display the value of the custom field in the user profile. The code example below adds a new field to the user profile page to display the phone number.

function wpsnippets_add_custom_field_to_user_profile( $user ) {
    ?>
    <table class="form-table">
        <tr>
            <th><label for="phone_number"><?php _e( 'Phone Number', 'textdomain' ); ?></label></th>
            <td>
                <input type="text" name="phone_number" id="phone_number" value="<?php echo esc_attr( get_the_author_meta( 'phone_number', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e( 'Please enter your phone number.', 'textdomain' ); ?></span>
            </td>
        </tr>
    </table>
    <?php
}
add_action( 'show_user_profile', 'wpsnippets_add_custom_field_to_user_profile' );
add_action( 'edit_user_profile', 'wpsnippets_add_custom_field_to_user_profile' );

The code above adds a new field to the user profile page to display the phone number. The value is retrieved using the get_the_author_meta() function and displayed in an input field. The user can edit the phone number and save the changes.

Last updated on September 24, 2023. Originally posted on September 27, 2023.

Leave a Reply

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