Home > Dev > PHP Security > Preventing Spam Registrations

Preventing Spam Registrations

This code will add a simple honeypot field to the registration form to prevent spam registrations.

// Add this to your theme's functions.php file

function add_honeypot_field() {
    echo '<p class="hidden-field"><label>Leave this field empty<input type="text" name="honeypot" value="" /></label></p>';
}
add_action('register_form', 'add_honeypot_field');

function check_honeypot_field($errors, $sanitized_user_login, $user_email) {
    if (!empty($_POST['honeypot'])) {
        $errors->add('spam_error', __('<strong>ERROR</strong>: Your registration was flagged as spam.'));
    }
    return $errors;
}
add_filter('registration_errors', 'check_honeypot_field', 10, 3);
Back