# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, spam, honeypot
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-honeypot/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Honeypot Field for Comment Spam" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
