Prevent Long spam comments in WordPress

Most of the time attackers and spammers tends to attack the site by adding spammy comment to your site. It is important that we keep eye on ping back and spammy comments. One thing that I always like to add to my theme as developer is keep track of the comments. If the comments length is too long it is important to drop it down.

Here is the filter that you can add to functions.php file to remove the comments that are longer than specified length. You could even add jquery to add little notifications to the comment text area to notify the users.

add_filter( 'pre_comment_content', 'sasweb_die_on_long_comment', 9999 );

function sasweb_die_on_long_comment( $text ) {
    if ( strlen($text) > 13000 ) {
            /*message*/ 'This comment is longer than the maximum allowed size and has been dropped.', 
            /*title*/ 'Comment Declined',
            /*args*/ array( 'response' => 413 )
    return $text;

About the Author


I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.