Block enumarated E-Mails

We had a lot of sign-ups using 07370230723@domain.com. And we wanted to block that. We used the following functions.php Snippet to block enumarated E-Mails through WordPress. Insert into your functions.php, f.ex. with Code Snippets.

function block_numeric_emails($email) {
    // Regular expression to check if the email username part is numeric
    if (preg_match('/^[0-9]+@/', $email)) {
        return new WP_Error('invalid_email', __('E-Mailadresse, die nur Nummern enthalten, sind nicht erlaubt. Die Aktion wurde blockiert.'));
    }
    return $email;
}

// Hook into the comment form validation
add_filter('preprocess_comment', 'block_numeric_emails_comment');
function block_numeric_emails_comment($commentdata) {
    $email = $commentdata['comment_author_email'];
    $result = block_numeric_emails($email);
    if (is_wp_error($result)) {
        wp_die($result->get_error_message());
    }
    return $commentdata;
}

// Hook into the user registration validation
add_filter('registration_errors', 'block_numeric_emails_registration', 10, 3);
function block_numeric_emails_registration($errors, $sanitized_user_login, $user_email) {
    $result = block_numeric_emails($user_email);
    if (is_wp_error($result)) {
        $errors->add('invalid_email', $result->get_error_message());
    }
    return $errors;
}

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *