Would you like to customize the error messages that appear during user registration? WPForms lets you create more helpful messages like “Username Already Exists” or “Email Already Exists” to guide your visitors through the registration process.
This guide will show you how to customize these validation messages using PHP.
Setting Up Your Registration Form
First, make sure you have installed the User Registration addon for WPForms. This addon provides two pre-made templates: User Login Form and User Registration Form. Since we’re focusing on registration messages, create a new form using the User Registration template.
These forms can display several types of validation messages to help guide your users. Let’s customize each type of message.
Changing the Validation Messages
In order to change the validation messages, you’ll need to add one of the snippets below to your site.
If you’re not sure how to add custom code, please see our guide on how to add code snippets to your site.
Username Already Exists Message
The default message is A user with that username already exists. Here’s how to customize it and add a login link:
/**
* Change the User Registration Form error message for the already existing username
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_username_exists( $msg ) {
// This is the message that would appear
$msg = __( 'A user with that username already exists. Please <a href="http://yourdomain/wp-admin/">log in here</a> to your account.', 'text-domain' );
return $msg;
}
add_filter( 'wpforms_user_registration_process_registration_process_username_exists_error_message', 'wpf_dev_user_registration_username_exists', 10, 1 );
Email Already Exists Message
The default message is A user with that email already exists. Here’s how to customize it with a login link:
/**
* Change the User Registration Form error message for already existing email address
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_email_exists( $msg ) {
// This is the message that would appear
$msg = __( 'A user with that username already exists. Please <a href="http://yourdomain/wp-admin/">log in here</a> to your account.', 'text-domain' );
return $msg;
}
add_filter( 'wpforms_user_registration_process_registration_process_user_email_exists_error_message', 'wpf_dev_user_registration_email_exists', 10, 1 );
Incorrect Password Message
The default message is Error: The password you entered for the username is incorrect. Lost your password? Here’s how to customize it with a password reset link:
/**
* Password is incorrect
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_login_error( $msg ) {
// This is the message that would appear in the email
$msg = __('Sorry something went wrong! ', 'text-domain');
$msg .= '<a href=\\"http://www.website.com\\">';
$msg .= __('Would you like to reset your password?', 'text-domain');
$msg .= '</a>';
return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 1 );
Custom Login Form Messages
You can also customize messages for the user login form. Here’s how to create a generic message for all forms:
/**
* Change the User Login Form error message
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_user_registration_login_error($msg) {
// This is the message that would appear in the email
$msg = __( 'Sorry something went wrong! ', 'text-domain' );
$msg .= '<a href="'.esc_url( wp_lostpassword_url() ).'">';
$msg .= __( 'Would you like to reset your password?', 'text-domain' );
$msg .= '</a>';
return $msg;
}
add_filter( 'wpforms_user_registration_process_login_process_wp_error_message', 'wpf_dev_user_registration_login_error', 10, 2 );
Form-Specific Error Messages
You can set different error messages for different forms. Here’s how to customize messages based on form ID. If you need help finding your form IDs, check out our guide on how to find form and field IDs.
/**
* Change the User Login Form error message specific per form
*
* @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
*/
function wpf_dev_form_login_errors( $fields, $entry, $form_data ) {
$form_id = (int) $form_data[ 'id' ];
add_filter(
'wpforms_user_registration_login_error',
function ( $message, $code ) use ( $form_id ) {
// This if for form ID 525
if ( $form_id === 525) {
// This is the specific message for form ID 525
return __( 'The error message here for form #525', 'text-domain' );
}
// This is for form ID 526
if ( $form_id === 526) {
// This is the specific message for form ID 526
return __( 'The error message here for form #526', 'text-domain' );
}
return $message;
},
10,
2
);
}
add_action( 'wpforms_process', 'wpf_dev_form_login_errors', 9, 3 );
Frequently Asked Questions
Q: Can I show all of the error messages at once?
A: No, PHP is executed in the order the snippets are added or in order of the priority set on the function. If you added the wpforms_user_registration_username_exists snippet first on your site, once the form is submitted, it will execute your snippets in the order they were added and display the message for the first snippet it fails.
And that’s all you need to add additional validation messages for the User Registration addon. Would you like to automatically log your users in once they’ve completed registration? Check out our guide on how to automatically log in users after registration or learn how to add custom user meta fields to registration forms.