Skip to content

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

Minimum comment length

Comments shorter than 20 characters are refused with a friendly message. Stops “Nice post!” spam and empty replies.

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

add_filter(
	'preprocess_comment',
	static function ( $comment ) {
		// Only people's comments: pingbacks, trackbacks and notes from plugins pass.
		if ( ! empty( $comment['comment_type'] ) && 'comment' !== $comment['comment_type'] ) {
			return $comment;
		}
		$minimum = 20;
		if ( mb_strlen( trim( wp_strip_all_tags( (string) $comment['comment_content'] ) ) ) < $minimum ) {
			wp_die(
				esc_html( sprintf( 'Your comment is a little short. Please write at least %d characters.', $minimum ) ),
				esc_html__( 'Comment Submission Failure' ),
				array( 'response' => 200, 'back_link' => true )
			);
		}
		return $comment;
	}
);

Good to know

Change $minimum to suit your site, and translate the message.

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.