If you have a personal blog or a news site, you may want to customize the comments on your page.
Whether it is because you receive extremely long comments or simply because you want to set a maximum number of characters.
In this guide, we show how to limit the minimum and maximum number of characters in your WordPress comments.
The process consists of adding a code snippet to the
functions.php file of the theme currently active in your WordPress. To do this, you need to access your hosting files via FTP.
How to access FTP
Once you are inside your FTP, navigate to the files of the theme installed on your WordPress. The default path is /web/wp-content/themes/yourthemename.
Inside your theme folder, you will see a file called “functions.php”. Edit this file to add the following code:
function wpbody_longitud_comentario($comentario) {
if ( mb_strlen( $comentario['comment_content'] ) > 1000 ) {
wp_die('Comment exceeds the allowed limit.');
} elseif ( mb_strlen( $comentario['comment_content'] ) < 100 ) {
wp_die('Comment is too short!');
}
return $comentario;
}
Below is a screenshot of the code added from webFTP. It must be added at the very end of the file:
This code is just an example. It will reject comments longer than 1000 characters and those shorter than 100 characters.
If the comment exceeds 1000 characters, it will display the message “Comment exceeds the allowed limit.” If fewer than 100 characters are entered, it will display “Comment is too short!”
If you want to edit the message displayed, simply modify the text inside the code above. The same applies to the minimum and maximum character limits—if you want to set a specific value, just change the numbers in the code accordingly.
When testing the minimum and maximum limits, WordPress will display the following error messages:
Comment with fewer than 100 characters:
Comment with more than 1000 characters: