Limit WordPress comment length using functions.php

Send Us a Sign! (Contact Us!)
Word PDF XPS XML

The following method is for users who don’t mind dealing with code. We will add a filter hook to preprocess_comment. This filter is run before WordPress saves any comments to database or runs any other pre-processing on submitted comments. We will use it to check the comment length.

If it is above or below the set comment length parameters, then we will show users an error message. Simply add this code to your theme’s functions.php file or a site-specific plugin.

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
function wpb_preprocess_comment($comment) {
    if ( strlen( $comment['comment_content'] ) > 5000 ) {
       wp_die('Comment is too long. Please keep your comment under 5000 characters.');
    }
if ( strlen( $comment['comment_content'] ) < 60 ) {
       wp_die('Comment is too short. Please use at least 60 characters.');
    }
    return $comment;
}