The is_email WordPress PHP Filter allows you to modify the result of the is_email WordPress PHP function – for example if you want to customise what is a valid email address.
Usage
add_filter('is_email', 'your_custom_function', 10, 3);
function your_custom_function($is_email, $email, $context) {
// Your custom code here
return $is_email;
}
Parameters
- $is_email (string|false): The email address if it successfully passed the is_email() checks, false otherwise.
- $email (string): The email address being checked.
- $context (string): Context under which the email was tested.
More information
See WordPress Developer Resources: is_email
Examples
Allow only specific domain for email addresses
Allow email addresses only from the “example.com” domain.
add_filter('is_email', 'allow_specific_domain', 10, 3);
function allow_specific_domain($is_email, $email, $context) {
$allowed_domain = 'example.com';
$email_domain = substr(strrchr($email, "@"), 1);
if ($email_domain === $allowed_domain) {
return $is_email;
} else {
return false;
}
}
Block a specific email address
Block an email address “[email protected]“.
add_filter('is_email', 'block_specific_email', 10, 3);
function block_specific_email($is_email, $email, $context) {
$blocked_email = '[email protected]';
if ($email === $blocked_email) {
return false;
} else {
return $is_email;
}
}
Change the minimum length of an email address
Allow email addresses with a minimum length of 6 characters.
add_filter('is_email', 'change_minimum_email_length', 10, 3);
function change_minimum_email_length($is_email, $email, $context) {
$min_length = 6;
if (strlen($email) >= $min_length) {
return $is_email;
} else {
return false;
}
}
Log invalid email addresses
Log invalid email addresses to a file.
add_filter('is_email', 'log_invalid_email', 10, 3);
function log_invalid_email($is_email, $email, $context) {
if (!$is_email) {
error_log("Invalid email: $email (Context: $context)\n", 3, 'invalid_emails.log');
}
return $is_email;
}
Convert email addresses to lowercase
Ensure that all email addresses are lowercase.
add_filter('is_email', 'convert_email_to_lowercase', 10, 3);
function convert_email_to_lowercase($is_email, $email, $context) {
if ($is_email) {
return strtolower($email);
} else {
return $is_email;
}
}