register_form
Fires when the registration form is displayed on the WordPress site.
add_action( 'register_form', $callback, 10, 1 );
Description
Fires after the default registration form fields are output. Developers can use this hook to add custom fields to the registration form, allowing for extra user information collection during signup.
Usage
add_action( 'register_form', 'your_function_name', 10, 1 );
Examples
/**
* Add a custom field to the WordPress registration form.
* This function hooks into the 'register_form' action to display
* an additional field for users to enter their company name.
*
* @param void
* @return void
*/
add_action( 'register_form', 'my_custom_registration_field' );
function my_custom_registration_field() {
// Check if the 'company' field is already set from a previous submission
// This is useful if there's an error during registration and the form reloads.
$company_name = ( ! empty( $_POST['company_name'] ) ) ? sanitize_text_field( $_POST['company_name'] ) : '';
?>
<p>
<label for="company_name"><?php esc_html_e( 'Company Name', 'your-text-domain' ); ?><br>
<input type="text" name="company_name" id="company_name" class="input" value="<?php echo esc_attr( $company_name ); ?>" size="25" />
</label>
</p>
<?php
}
/**
* Validate and save the custom field data during registration.
* This function hooks into the 'register_post' action, which is triggered
* when the registration form is submitted. It validates the custom field
* and adds it to the user's metadata if valid.
*
* @param WP_User $user_id The ID of the newly created user.
* @return void
*/
add_action( 'register_post', 'my_validate_and_save_custom_registration_field', 10, 1 );
function my_validate_and_save_custom_registration_field( $user_id ) {
// Check if the 'company_name' field was submitted and is not empty.
if ( isset( $_POST['company_name'] ) && ! empty( $_POST['company_name'] ) ) {
// Sanitize the input to prevent security issues.
$company_name = sanitize_text_field( $_POST['company_name'] );
// Further validation if needed, e.g., checking for specific characters or length.
// For this example, we'll just ensure it's not empty after sanitization.
if ( ! empty( $company_name ) ) {
// Save the sanitized company name as user meta data.
update_user_meta( $user_id, 'company_name', $company_name );
}
}
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/templates/register-form.php:103
placeholder="<?php echo esc_html__( 'Registration Code', 'uncanny-learndash-groups' ); ?>"
value="<?php $template->the_posted_value( 'code_registration' ); ?>" size="20"/>
</p>
<?php } ?>
<p class="tml-registration-confirmation"
id="reg_passmail<?php $template->the_instance(); ?>"><?php echo apply_filters( 'tml_register_passmail_template_message', __( 'Registration confirmation will be e-mailed to you.', 'uncanny-learndash-groups' ) ); ?></p>
<?php do_action( 'register_form' ); ?>
<p class="tml-submit-wrap">
<input type="submit" name="wp-submit" class="btn btn-default"
id="wp-submit<?php $template->the_instance(); ?>"
value="<?php esc_attr_e( 'Register', 'uncanny-learndash-groups' ); ?>"/>
<input type="hidden" name="redirect_to" value="<?php $template->the_redirect_url( 'register' ); ?>"/>
<input type="hidden" name="instance" value="<?php $template->the_instance(); ?>"/>
<input type="hidden" name="action" value="register"/>