Limit WordPress Comments Length [Manually]

One of the best ways to interact with your visitors is to allow them to leave comments on your site, and because WordPress was developed as blogging software, it has an extensive comments system. In this article, we’ll show you how you can limit the wordpress comments length so that no one can actually spam your site with lengthy comments.

Also Read: Change Default WordPress Login Error Messages

By default, WordPress sets up the following parameters:

  • The comment form and existing comments will appear on any post or page.
  • To post a comment, visitors must fill out their name and e-mail address.
  • The ability to comment is always open – there’s no time limit on the comments for a particular post or page.
  • All comments are held for approval unless the commenter has been approved before.
  • The e-mail address listed in WordPress administration will be notified if any comments are posted or if any comments are held for moderation.
  • Any comment is held for moderation if it contains more than two links. No words or web addresses are blacklisted.
  • Avatars (small graphics or photos) are displayed with each comment and a default avatar is selected if the visitor doesn’t provide one.

Limiting the comment length is not a bad idea because it actually adds an extra layer of security into your website. So basically you need to add a code into your theme’s functions.php file.

Limiting Comments Length using Code Snippet

Here, we need to add a filter hook called as “preprocess_comment” which prevents lengthy comments to saves into the database. Simply add the following code into your theme’s functions.php file. (Pastebin Link)

add_filter( ‘preprocess_comment’, ‘wpb_preprocess_comment’ );

function wpb_preprocess_comment($comment) {
if ( strlen( $comment[‘comment_content’] ) > 5000 ) {
wp_die(‘Comment is too long. Please keep your comment under 5000 characters.’);
}
if ( strlen( $comment[‘comment_content’] ) < 60 ) {
wp_die(‘Comment is too short. Please use at least 60 characters.’);
}
return $comment;
}

You may also like:

Sarcastic Writer

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

Related Posts