Home / comment / Require minimum comment length in wordpress
  • How to Require minimum comment length in wordpress

    wordpress-require-minimum-comment-length

    Add this great snippet by Paul to the functions.php of your wordpress theme and require a minimum comment length. Anything under the limit will return an error message using wp_die “All comments must be at least 20 characters long.”

    Download an easy to install and use version of this snippet with admin controls for easy use. ( click code to copy )

    wordpress snippet : PHP - functions.php

    <>
    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;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    preprocess_comment, add_filter, wp, wp_die,
  • kachan64

    Can I add custom div style for error messages?

    • Unfortunately not that I’m aware of. This error page is not from your template but from wordpress core. I have seen some hacks but all require hacking core files and this is not a good idea.