Using WordPress ‘pre_user_email’ PHP filter

‘pre_user_email’ is a WordPress PHP filter that allows you to modify a user’s email before the user is created or updated in the database. This can be useful for sanitizing, validating or transforming the email address in various scenarios.

Usage

To use this filter, create a function that will manipulate the email address and then hook it to the ‘pre_user_email’ filter. The function should accept one parameter: $raw_user_email.

Here’s a code example:

function my_custom_email_filter( $raw_user_email ) {
    // Manipulate the email address here
    return $raw_user_email;
}
add_filter( 'pre_user_email', 'my_custom_email_filter' );

Parameters

  • $raw_user_email (string): The user’s email that you want to modify.

Examples

Lowercase Email Addresses

function lowercase_user_email( $raw_user_email ) {
    return strtolower( $raw_user_email );
}
add_filter( 'pre_user_email', 'lowercase_user_email' );

In this example, the lowercase_user_email() function converts the user’s email address to lowercase before it’s saved. This ensures consistency in the database and can help prevent issues with case-sensitive email systems.

Remove Spaces

function remove_spaces_from_email( $raw_user_email ) {
    return str_replace( ' ', '', $raw_user_email );
}
add_filter( 'pre_user_email', 'remove_spaces_from_email' );

This example removes any spaces in the email address before it’s saved, which can be helpful in case users accidentally include spaces while typing their email addresses.

Replace Custom Domain

function replace_custom_domain( $raw_user_email ) {
    return str_replace( '@olddomain.com', '@newdomain.com', $raw_user_email );
}
add_filter( 'pre_user_email', 'replace_custom_domain' );

In this scenario, the replace_custom_domain() function replaces the old domain in the email address with a new domain before saving the user’s email. This can be useful when migrating users from one domain to another.

Append Domain if Missing

function append_domain_if_missing( $raw_user_email ) {
    if ( strpos( $raw_user_email, '@' ) === false ) {
        return $raw_user_email . '@example.com';
    }
    return $raw_user_email;
}
add_filter( 'pre_user_email', 'append_domain_if_missing' );

In this example, the append_domain_if_missing() function checks if the email address has a domain, and if it doesn’t, it appends a default domain. This can be helpful when users only provide their username, and you want to assume a default email domain.

Validate Email Format

function validate_user_email_format( $raw_user_email ) {
    if ( ! filter_var( $raw_user_email, FILTER_VALIDATE_EMAIL ) ) {
        return '';
    }
    return $raw_user_email;
}
add_filter( 'pre_user_email', 'validate_user_email_format' );

In this example, the validate_user_email_format() function checks if the provided email address is in a valid format. If not, it returns an empty string, which will prevent the user from being created or updated with an invalid email address.