Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Spam trap in the comment form

Adds a hidden field that people never see but bots fill in. Comments that fill it in are refused. No captcha, nothing sent to other companies.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Category Comments
  • Tested with WordPress 7.1
PHP
<?php

// A field hidden from people (and from screen readers); bots fill every field.
$snipfire_trap = static function () {
	echo '<p class="snipfire-hp" aria-hidden="true" style="position:absolute;left:-9999px;height:0;overflow:hidden">'
		. '<label>Leave this empty <input type="text" name="snipfire_hp_website" value="" tabindex="-1" autocomplete="off"></label></p>';
};
add_action( 'comment_form_after_fields', $snipfire_trap );
add_action( 'comment_form_logged_in_after', $snipfire_trap );

add_filter(
	'preprocess_comment',
	static function ( $comment ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Comment form, checked by WordPress.
		if ( ! empty( $_POST['snipfire_hp_website'] ) ) {
			wp_die( esc_html__( 'Comment Submission Failure' ), esc_html__( 'Comment Submission Failure' ), array( 'response' => 403 ) );
		}
		return $comment;
	}
);

In these packs

  • Comments: Fewer spam comments and more control: close old posts, set minimum and maximum lengths, a spam trap, no links.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.