Skip to content

How To Set Minimum Comment Length In WordPress

Don’t you hate when you write a length post with a lot of content and all the comments on your posts are just couple of words long with the backlink to their website? I hate it, as some people just comment just to get a link back to their own website. Their comments will not be much long, and will consist things like “nice post”, “thanks”, “you’re awesome” and all the crappy little words.

Or if you’re running an e-commerce website or a website with reviews or support, it would be a nice idea to set a minimum comment length of your WordPress comments. People with fewer letters than minimum setting will get an error message and will be requested to make their comment more length.

Just add following snippet to your current theme’s functions.php file:

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

Replace 20 in the above code with your custom value.

Leave a Reply

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