How to limit the minimum/maximum character value in Wordpress comments

How to limit the minimum/maximum character value in Wordpress comments

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:


    • Related Articles

    • Turn comments on and off in WordPress

      WordPress is a very complex CMS that allows you to customize your website and its pages in many different ways. If you have a personal blog or publish news/updates on your website, you will likely want to enable comments on that page. In this guide, ...
    • Most common Wordpress errors

      WordPress is a CMS application with a wide range of different applications and functions, which means there are also many errors that can be generated by them. In this guide, we show the most common and generic errors that WordPress may display: ...
    • How to limit access to wordpress login using ips

      One of the most common attack attempts against any WordPress-based website is trying to access the login page through brute force attacks or by overwhelming it with repeated requests. One way to protect your login area is to restrict access to the ...
    • Add cookie policy in Wordpress

      Nowadays, it is very common to find websites that use cookies to collect information about user behavior on the web. WordPress, being one of the most widely used content management systems (CMS) in the world, also allows users to use cookies on their ...
    • How to speed up our WordPress

      In many cases our WordPress websites take a long time to load, or we simply want them to be as fast as possible. By following these steps, we can achieve the maximum speed that our website allows: Check website load time using an online tool. ...