Do you want to change the validation messages for your WPForms User Registration addon? You can provide more detailed messages, like Username Already Exists or Email Already Exists, to help your visitors when they complete the registration form. In this tutorial, we’ll show you how to use PHP to create these custom validation messages.
Creating the form
After installing the User Registration addon for WPForms, you will have access to some pre-made templates: User Login Form and User Registration Form.
This tutorial focuses on the messages that might appear during the registration process, so we’ll create a new user registration form. If you need help creating this form, refer to the provided documentation.
Along with the standard validation messages, these forms can display specific error messages as needed.
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 need help with how to add snippets to your site, please see this tutorial.
Username already exists error
By default, this error will display: “A user with that username already exists.” In the code example below, we modify this text and add a link to the login.
/** * 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 error
By default, this error will display: “A user with that email already exists.” In the code example below, we modify this text and add a link to the login.
/** * 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 error
By default, this error will display: “Error: The password you entered for the username
/** * 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 );
When adding a URL in PHP, you need to escape the URL which is why there are 2 slashes before the URL and after.
Please keep in mind if you change the password reset URL, the user wouldn’t be able to automatically reset their own password through WordPress.
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? Please review our tutorial on How to Automatically Log in Users After Registration.
Reference Filters
- wpforms_user_registration_process_registration_process_user_email_exists_error_message
- wpforms_user_registration_process_login_process_wp_error_message
- wpforms_user_registration_process_registration_process_username_exists_error_message
FAQ
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.
Q: Can you change messages for the user login form?
A: Absolutely! By default, the user login form will display hints much like the traditional WordPress login form. By using the code snippet below, you can create a more generic message without a hint 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 );
You can even set a specific error message for all login errors by form ID. To achieve this use this snippet and change the form IDs and messages to your liking. If you need assistance in locating your form ID numbers, please review this tutorial.
/** * 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 );