Using WordPress ‘is_email()’ PHP function

The is_email() WordPress PHP function verifies if an email address is valid.

Usage

$is_valid = is_email( '[email protected]' );

Parameters

  • $email (string) – The email address to verify.
  • $deprecated (bool) – Optional. Deprecated parameter. Default: false.

More information

See WordPress Developer Resources: is_email

Examples

Check if an email is valid

$email = '[email protected]';
if ( is_email( $email ) ) {
    echo '**' . $email . '** is a valid email address.';
} else {
    echo '**' . $email . '** is not a valid email address.';
}

Validate email addresses in an array

$emails = array( '[email protected]', 'invalid@email' );
foreach ( $emails as $email ) {
    if ( is_email( $email ) ) {
        echo '**' . $email . '** is a valid email address.' . PHP_EOL;
    } else {
        echo '**' . $email . '** is not a valid email address.' . PHP_EOL;
    }
}

Filter out invalid email addresses

$emails = array( '[email protected]', 'invalid@email', '[email protected]' );
$valid_emails = array_filter( $emails, 'is_email' );

print_r( $valid_emails );

Validate email addresses in a form submission

$email = $_POST['email'];
if ( ! is_email( $email ) ) {
    echo 'Please enter a valid email address.';
} else {
    echo 'Thank you for submitting a valid email address!';
}

Validate email addresses in a CSV file

$file = fopen( 'emails.csv', 'r' );
while ( ( $data = fgetcsv( $file ) ) !== FALSE ) {
    $email = $data[0];
    if ( is_email( $email ) ) {
        echo '**' . $email . '** is a valid email address.' . PHP_EOL;
    } else {
        echo '**' . $email . '** is not a valid email address.' . PHP_EOL;
    }
}
fclose( $file );