Honeypot Field for Comment Spam
Adds a hidden field to the comment form that people never see but many bots fill in. Comments that fill it are rejected before reaching the database.
Opens WPIDE on
Code
add_action( 'comment_form', function () {
if ( is_user_logged_in() ) {
return;
}
echo '<p class="wpide-hp" style="position:absolute;left:-10000px;top:auto;width:1px;height:1px;overflow:hidden;" aria-hidden="true">';
echo '<label for="wpide_hp_field">Leave this field empty</label>';
echo '<input type="text" name="wpide_hp_field" id="wpide_hp_field" value="" tabindex="-1" autocomplete="off" />';
echo '</p>';
} );
add_filter( 'preprocess_comment', function ( $commentdata ) {
$type = isset( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '';
if ( is_user_logged_in() || ( '' !== $type && 'comment' !== $type ) ) {
return $commentdata;
}
if ( ! empty( $_POST['wpide_hp_field'] ) ) {
wp_die( 'Your comment could not be submitted.', 'Comment rejected', array( 'response' => 403, 'back_link' => true ) );
}
return $commentdata;
}, 1 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.