Some spambots are designed to leave one-word comments like “Hello”. Automated spam blocking plugins cannot flag this comment as spam because it is seemingly harmless. By adding a mandatory limit on the number of characters, you can keep these bots at bay!‎

In this little tutorial we will show you a little trick to limit the minimum and maximum number on the number of characters of your comments without a plugin.

limit the minimum and maximum length of wordpress comments

To add a character limit to the comments field, you must modify the code in the functions.php.‎ file.

Go to Appearance > Theme Editor. Now open the functions.php file. Add the following code at the end of the file: ‎

add_filter( ‘preprocess_comment’, ‘wpb_preprocess_comment’ );
function wpb_preprocess_comment($comment) {
 if ( strlen( $comment[‘comment_content’] ) > 5000 ) {
 wp_die(‘Le commentaire est trop long. Veuillez garder votre commentaire en dessous de 100 mots.’)
}
if ( strlen( $comment[‘comment_content’] ) < 60 ) {
wp_die(‘'Le commentaire est trop court.’);  
}
return $comment;
}

‎Now click on “Update File”. If someone tries to add a comment below and/or above the set limit, a message is displayed.‎

If you liked this article, please subscribe to our reddit community to discuss it. You can also find us on Twitter and Facebook.

Tagged in:

About the Author

SAKHRI Mohamed

The blog of a computer enthusiast who shares news, tutorials, tips, online tools and software for Windows, macOS, Linux, Web designer and Video games.

View All Articles