Minimum comment length
Comments shorter than 20 characters are refused with a friendly message. Stops “Nice post!” spam and empty replies.
<?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.