Using WordPress ‘registration_errors’ PHP filter

The 'registration_errors' filter is used to filter errors encountered during a new user registration process. By utilizing this filter, you can create custom validation rules for user registration.

Usage

add_filter( 'registration_errors', 'my_custom_registration_errors', 10, 3 );

function my_custom_registration_errors( $errors, $sanitized_user_login, $user_email ) {
    // Your custom validation logic here

    return $errors;
}

Parameters

  • $errors WP_Error
    • A WP_Error object containing any errors encountered during registration.
  • $sanitized_user_login string
    • User’s username after it has been sanitized.
  • $user_email string
    • User’s email.

Examples

Disallow usernames containing numbers

add_filter( 'registration_errors', 'disallow_numbers_in_username', 10, 3 );

function disallow_numbers_in_username( $errors, $sanitized_user_login, $user_email ) {
    if ( preg_match( '/d/', $sanitized_user_login ) ) {
        $errors->add( 'username_contains_numbers', 'Usernames cannot contain numbers.' );
    }

    return $errors;
}

In this example, we disallow usernames containing numbers by using the preg_match() function to check for any digits. If a digit is found, an error is added to the $errors object.

Require user email to be from a specific domain

add_filter( 'registration_errors', 'require_email_domain', 10, 3 );

function require_email_domain( $errors, $sanitized_user_login, $user_email ) {
    $allowed_domain = 'example.com';
    $email_domain = substr( strrchr( $user_email, "@" ), 1 );

    if ( $email_domain !== $allowed_domain ) {
        $errors->add( 'invalid_email_domain', 'Email must be from the example.com domain.' );
    }

    return $errors;
}

In this example, we require the user’s email to be from the “example.com” domain. If the email is not from the allowed domain, an error is added to the $errors object.

Minimum username length requirement

add_filter( 'registration_errors', 'minimum_username_length', 10, 3 );

function minimum_username_length( $errors, $sanitized_user_login, $user_email ) {
    $min_length = 5;

    if ( strlen( $sanitized_user_login ) < $min_length ) {
        $errors->add( 'short_username', 'Usernames must be at least 5 characters long.' );
    }

    return $errors;
}

In this example, we enforce a minimum username length requirement. If the username is shorter than the required length, an error is added to the $errors object.

Disallow special characters in usernames

add_filter( 'registration_errors', 'disallow_special_chars_in_username', 10, 3 );

function disallow_special_chars_in_username( $errors, $sanitized_user_login, $user_email ) {
    if ( preg_match( '/[^a-zA-Z0-9_]/', $sanitized_user_login ) ) {
        $errors->add( 'username_special_chars', 'Usernames can only contain letters, numbers, and underscores.' );
    }

    return $errors;
}

In this example, we disallow special characters in usernames using the preg_match() function to check for any characters that are not letters, numbers, or underscores. If a special character is found, an error is added to the $errors object.

Check if the email address is already used by another user

add_filter( 'registration_errors', 'check_email_already_used', 10, 3 );

function check_email_already_used( $errors, $sanitized_user_login, $user_email ) {
    if ( email_exists( $user_email ) ) {
        $errors->add( 'email_exists', 'This email address is already in use by another user.' );
    }

    return $errors;
}

In this example, we check if the provided email address is already used by another user. If the email is already in use, an error is added to the $errors object.

These examples demonstrate how to use the apply_filters('registration_errors', WP_Error $errors, string $sanitized_user_login, string $user_email) filter to create custom validation rules for user registration in WordPress. By incorporating these custom validation rules, you can provide a more tailored user registration experience for your website visitors.