Define Minimum Comment Length in WordPress Comments

x theme

Here, we’ll show you how to define minimum comment length in WordPress. Just paste the code to the functions.php file of your theme. You just have to define the minimum length of WordPress comments and that’s it. WordPress will handle everything by itself using wp_die, if a comment doesn’t reach your minimum criteria.

add_filter( 'execute_comment', 'minimum_length_of_comment' );
function minimum_length_of_comment( $commentdata ) {
$minimuLengthOfComment = 20;
if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimuLengthOfComment ){
wp_die( 'All comments must be at least ' . $minimuLengthOfComment . ' characters long.' );
}
return $commentdata;
}

Thanks to Paul Underwood for this tip.

Leave a Reply

Your email address will not be published. Required fields are marked *