How to Change Validation Messages for User Registration Addon

Introduction

Would you like to change the validation messages for your WPForms User Registration Addon? You can display more detailed validation messages such as User Name Already Exists or Email Already Exists messages to your visitors when they complete your registration form. In this tutorial, we’ll show you how to use PHP to create these additional validation messages.

Creating the form

After installing the User Registration addon for WPForms, two new templates will become available: User Login Form and User Registration Form.

Since this tutorial will be focusing on the messages that could appear during the registration process, we’re going to create a new user registration form. If you need any help in creating

In addition to using standard validation messages, both of these forms will display content-specific errors when needed.

If you need any assistance in how to create a user registration form, please review this documentation.

create your user registration form with WPForms

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 in 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 let the user reset their password (in case they registered earlier with this username and forgot).

/**
 * Change the User Registration Form error message for the already existing email address.
 * 
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 *
 */

function wpf_dev_user_registration_username_exists( $msg ) {

	$msg = 'A user with that username already exists. Need to <a href="';
	$msg .= esc_url( wp_lostpassword_url() );
	$msg .= '"> reset your password?</a>';

	return $msg;
}

add_filter( 'wpforms_user_registration_username_exists', '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 let the user reset their password (in case they registered earlier with this email address and forgot).

/**
 * Change the User Registration Form error message for already existing username.
 * 
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 *
 */

function wpf_dev_user_registration_email_exists( $msg ) {

	$msg = 'A user with that email already exists. Need to <a href="';
	$msg .= esc_url( wp_lostpassword_url() );
	$msg .= '"> reset your password?</a>';

	return $msg;

}

add_filter( 'wpforms_user_registration_email_exists', 'wpf_dev_user_registration_email_exists', 10, 1 );

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.

Filter References:

FAQ

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.

/**
 * 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) {

	$msg = 'Sorry something went wrong! <a href="';
	$msg .= esc_url( wp_lostpassword_url() );
	$msg .= '">Reset your password?</a>';

	return $msg;

}

add_filter( 'wpforms_user_registration_login_error', 'wpf_dev_user_registration_login_error', 10, 1 );

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.

/**
 * Change the User Login Form error message specific per form.
 * 
 * @link https://wpforms.com/developers/change-validation-messages-for-user-registration-addon/
 *
 */

function process_form( $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';
         }
		  
	 // 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';
         }
		  
         return $message;
      },

      10,
      2
   );

}
add_action( 'wpforms_process', 'process_form', 9, 3 );