Set WordPress comment_content length

Hi all is there a way I can limit the length of comment_content in WordPress. I am creating a view of the last 5 comments of a page but want to limit the length.

This is the code I have so far:

<?php $args = array ( 'post_id' => '225', 'number' => '5', 'orderby' => 'date', 'order' => 'DESC', 'comment_approved' => '1' );

     $comments = get_comments($args);
       foreach($comments as $comment) :
        echo('<div class="sidebarNewsTitle">'. $comment->comment_author . ' said : "' . strip_tags($comment->comment_content). '..."</div>');
       endforeach;
?>

Related posts

Leave a Reply

3 comments

  1. add_action( 'preprocess_comment', 'ct_minimum_comment_length', 8 );
    
    function ct_minimum_comment_length( $commentdata ){
        $minlength = 20;//minimal length you want to limit the comment content
        preg_match_all( '/./u', trim( $commentdata['comment_content'] ), $maxlength );
        $maxlength = count( $maxlength[0] );
        if( $maxlength < $minlength ) {
            wp_die( sprintf(_('Come on buddy, say at least %s characters', 'ct'), $minlength ));
        }
        return $commentdata;
    }
    

    Reference:
    http://clonetemplates.com/codex/limit-minimal-length-comment-content-wordpress.html/
    http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment

  2. I guess you could try to limit length of comments with wp_trim_words() function which allows you to set the character/words limit for a specific block of content.

    To learn more about this function check this article.

    But it is better to make your users aware of the limit, so using this plugin would probably be a better choice.

    With this plugin you set:

    limit on the length of comments left in the comment form, with a
    character countdown displayed for the user and dynamically updated
    with each keypress.