User registration hooks: validate extra fields, save them, send a welcome email
The registration form on wp-login.php?action=register asks only for a username and an email. Three hooks let you extend it: register_form prints extra fields, the registration_errors filter validates them and can stop the registration, and user_register runs after the account was created. With profile_update you can also react to later changes of the profile.
Problem: New users must enter a company name when they register. It must be required, at most 100 characters, saved in user meta, and used in the welcome email.
Solution: Print the field on register_form, validate it in registration_errors, save it on user_register, and change the welcome email with the wp_new_user_notification_email filter instead of sending a second one.
The field
<?php
add_action( 'register_form', function () {
$company = isset( $_POST['ha_company'] ) ? sanitize_text_field( wp_unslash( $_POST['ha_company'] ) ) : '';
?>
<p>
<label for="ha_company"><?php esc_html_e( 'Company', 'textdomain' ); ?></label>
<input type="text" name="ha_company" id="ha_company" class="input" maxlength="100"
value="<?php echo esc_attr( $company ); ?>" required>
</p>
<?php
} );
Validation
<?php
add_filter( 'registration_errors', function ( $errors, $sanitized_user_login, $user_email ) {
$company = isset( $_POST['ha_company'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['ha_company'] ) ) ) : '';
if ( '' === $company ) {
$errors->add( 'ha_company_required', __( '<strong>Error</strong>: Please enter your company.', 'textdomain' ) );
} elseif ( mb_strlen( $company ) > 100 ) {
$errors->add( 'ha_company_length', __( '<strong>Error</strong>: The company name is too long.', 'textdomain' ) );
}
return $errors;
}, 10, 3 );
Any error added here stops the registration and is shown above the form, with the values the user typed kept in the fields.
Saving
<?php
add_action( 'user_register', function ( $user_id ) {
if ( isset( $_POST['ha_company'] ) ) {
update_user_meta( $user_id, 'ha_company', sanitize_text_field( wp_unslash( $_POST['ha_company'] ) ) );
}
} );
user_register fires for every new user, also for users created in the admin, by WooCommerce, by an import with wp_insert_user() or through the REST API. Then there is no ha_company in $_POST, which is why the check is there.
The welcome email
WordPress already sends a “set your password” email to new users. Instead of sending a second email, change that one:
<?php
add_filter( 'wp_new_user_notification_email', function ( $email, $user, $blogname ) {
$company = get_user_meta( $user->ID, 'ha_company', true );
$email['subject'] = sprintf( __( 'Welcome to %s', 'textdomain' ), $blogname );
$email['message'] = sprintf( __( "Hello %1\$s from %2\$s,\n\n", 'textdomain' ), $user->display_name, $company ) . $email['message'];
return $email;
}, 10, 3 );
The original message contains the link to set the password; keep it and add your text before it.
Reacting to profile changes
<?php
add_action( 'profile_update', function ( $user_id, $old_user_data ) {
$user = get_userdata( $user_id );
if ( $user->roles !== $old_user_data->roles ) {
error_log( sprintf( 'User %d: roles %s -> %s', $user_id, implode( ',', $old_user_data->roles ), implode( ',', $user->roles ) ) );
}
}, 10, 2 );
NOTE: The registration form is a favourite target of bots. Required custom fields stop some of them, but a site with open registration also needs real protection: a CAPTCHA, a honeypot field or email confirmation before the account becomes active. And with “Anyone can register” on, check that the default role in Settings → General is Subscriber.