You can limit comment length in your WordPress site using following simple code snippet. We used the filter preprocess_comment to set the limit. preprocess_comment is a filter-hook that is applied to the comment data prior to any other processing of the comment’s information when saving a comment data to the database.

If comment length is above $max_length , then an error message will be shown to the visitor.

add_filter( 'preprocess_comment', 'limit_comment_length' );

function limit_comment_length($comment) {

    $max_length = 3000; // Set your length here
    if ( strlen( $comment['comment_content'] ) > $max_length ) {
        wp_die('Please keep your comment under '.$max_length.' characters.');
    }
    
    return $comment;
}

Add this code snippet at the end of your theme’s function.php file and don’t forget to set the length.


Post Tags


About The Author


Leave a Reply

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

home