How to limit comment length in WordPress (simple tutorial)

WordPress's comments feature encourages readers to start a discussion around the content of a post on the site. However, you may find that some comments are too short or rather long and don't contribute much to the discussion. Don't worry, next we'll show you how to easily limit the length of comments in WordPress to make the discussion more focused and effective.

Image[1]-How to Limit Comment Length in WordPress (Simple Tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

Why limit the length of comments in WordPress?

An active comments section is crucial to creating a community atmosphere on a WordPress blog. Here, visitors can not only share their feedback, but also ask questions and even voice their opinions on a particular topic.

However, it is also important to note that not every comment is beneficial to us.

Sometimes overly brief comments are usually not very helpful to the community atmosphere of a WordPress blog. In most cases, such comments are spammy comments and the person posting the comment is just trying to get backlinks from your site. Comments that are too long are usually complaining or unfavorable to the site. They are not even related to the article. So we need to limit the length of comments on our website to create a community of WordPress blogs with a good atmosphere.

Image[2]-How to limit the length of comments in WordPress (simple tutorial) - Photon Flux | Professional WordPress repair service, worldwide, fast response

How to Limit Comment Length in WordPress

To limit the length of comments in WordPress, you can send a message to thefunctions.php fileAdd code.take note ofA small error in entering the code can cause problems with the site or even make it inaccessible.

To avoid this risk, we recommend using theWPCodeThis plugin to add code snippets. It is one of the most popular WordPress code plugins on the market for adding custom code safely and easily. Using it, you'll be able to manage your website more easily.

Install and activate the WPCode plugin.

Once activated, from the WordPress admin sidebar access theCode Snippets " + Add Code SnippetPage. Here, click on the "Use snippet" button under the "Add custom code (new snippet)" option.

Image [3] - How to Limit Comment Length in WordPress (Simple Tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

Clicking on it will take you to the "Create Custom Code Snippet" page. Here you can give the snippet a name, which will not appear on your website, so you can do whatever you want.

Then, in the upper right corner of the screen, there is a drop-down menu, select "PHP snippet" as the code type.

Image [4] - How to Limit Comment Length in WordPress (Simple Tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

Once you have done this, simply copy and paste the following code snippet into the Code Preview box:

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; }
}
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; }
}
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; } }

The way this code snippet works is this: it will run a filter before WordPress saves the comment to the database or pre-processes it. This filter checks the length of the comment and displays an error message if the comment is too short or too long. This snippet sets the comment length to a minimum of 60 characters and a maximum of 5000 characters by default.

If you want to customize the comment length limit, simply replace the number 5000 in the code with the maximum limit number you want.

Similarly, if you want to set a different minimum comment length, you can also replace the number 60 in the code with the value you want. This way, you can easily set different comment length requirements on your WordPress site.

It is also possible to change the message that will be displayed on your site when a user goes over or under the comment limit. Simply type the sentence to be displayed after the line in the code.wp_die

if( strlen( $comment!comment content'])>5800)wp die('comment is too long. please keep your comment under 5000 characters. );
}
f(strlen($comment['comment content'])<60){wp die('comment is too short, please use at least 60 characters.'); }
if( strlen( $comment!comment content'])>5800)wp die('comment is too long. please keep your comment under 5000 characters. );
}
f(strlen($comment['comment content'])<60){wp die('comment is too short, please use at least 60 characters.'); }
if( strlen( $comment!comment content'])>5800)wp die('comment is too long. please keep your comment under 5000 characters. ); } f(strlen($comment['comment content'])<60){wp die('comment is too short, please use at least 60 characters.'); }
Image[5]-How to limit comment length in WordPress (simple tutorial) - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response

After that, scroll down to the "Insert" section and select the "Auto Insert" mode.

Upon activation, the custom code will be automatically executed on the website.

Image [6] - How to limit comment length in WordPress (simple tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

If you only want to limit the length of comments on specific website pages, then you can do that too. Simply scroll down to the "Conditional Logic" section and toggle the "Enable Logic" switch. Select the Show option from the Conditional drop-down menu, and then click the +Add New Group button.

Image [7] - How to Limit Comment Length in WordPress (Simple Tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

Once clicked, a new tab will open. In this tab, you will need to select the "Page URL" option from the drop-down menu on the left.

After that, in the box on the right, enter the URL of the page on which you want to limit the length of your comment.

This way, only the page corresponding to the URL you just entered will have this code snippet enabled for limiting comment length.

Image [8] - How to limit comment length in WordPress (simple tutorial) - Photon Fluctuation Network | Professional WordPress Repair Service, Global Reach, Fast Response

Scroll back to the top of the page and toggle the "Inactive" switch to "Active".

Finally, click on the Save Clip button to store your settings.

Image [9] - How to limit comment length in WordPress (simple tutorial) - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response

Now, if a user enters fewer words in a comment than the minimum length you've set, an alert message is displayed on their screen.

Users cannot successfully post a comment until they have entered a comment that reaches a set minimum length.

Image [10] - How to limit comment length in WordPress (simple tutorial) - Photon Flux | Professional WordPress repair service, worldwide, fast response

Similarly, this message is displayed on the user's screen when they type a comment that exceeds the maximum limit. Helps reduce spam comments on the site.

Image [11]-How to Limit Comment Length in WordPress (Simple Tutorial) - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response

I hope this tutorial has helped you guys master how to set a comment length limit in WordPress.


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments