WordPress Snippets at WPcustoms

Require Minimum Comment Length

Paste this code into your WordPress functions.php file to require a minimum comment length. Adjust the ’20’ to set the required characters.


/**
 * Snippet Name: Require Minimum Comment Length
 * Snippet URL: http://www.wpcustoms.net/snippets/require-minimum-comment-length/
 */
 add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
    $minimalCommentLength = 20;
    if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength ){
    wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
    }
    return $commentdata;
}