如何在WordPress中限制评论长度

WordPress comments encourage discussions around your topic. However you may find that comments below a certain length or above a certain length are not very helpful. In this article, we will show you how to limit comment length in WordPress, so you can set both minimum and maximum comment length limit for your WordPress site.

WordPress评论鼓励围绕您的主题进行讨论。 但是,您可能会发现,一定长度以下或一定长度以上的注释不是很有帮助。 在本文中,我们将向您展示如何在WordPress中限制评论长度,因此您可以为WordPress网站设置最小和最大评论长度限制。

为什么限制WordPress中的评论长度? (Why limit comment length in WordPress?)

Set Comment Length Limits in WordPress

In our experience of moderating online discussions for the past decade, we have found that most helpful comments are above 60 characters and below 5000 characters in length.

在过去十年中主持在线讨论的经验中,我们发现最有用的评论的长度应大于60个字符,而长度应小于5000个字符。

When a person writes a one-word comment, it usually is not very helpful. In most cases, it is spam because the author is simply trying to earn a backlink from your site.

当一个人写一个单词的评论时,通常不是很有帮助。 在大多数情况下,这是垃圾邮件,因为作者只是试图从您的网站获得反向链接。

However when a person writes a comment above 5000 characters, its usually a rant / complaint that in most cases is not relevant to that particular article.

但是,当一个人写的评论超过5000个字符时,通常会引起抱怨/抱怨,即在大多数情况下与该特定文章无关。

By setting comment length limits in WordPress, you can improve the quality of your comments.

通过在WordPress中设置评论长度限制,您可以提高评论的质量。

Let’s take a look at how to control comment length in WordPress.

让我们看一下如何控制WordPress中的注释长度。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

There are two methods to limit comment length in WordPress. The first method requires you to install a plugin. The second method uses a simple code snippet that you can add to your site.

有两种方法可以限制WordPress中的注释长度。 第一种方法要求您安装插件。 第二种方法使用一个简单的代码片段,您可以将其添加到您的网站。

方法1:使用插件限制评论长度 (Method 1: Limiting Comment Lenght Using a Plugin)

First thing you need to do is install and activate Control Comment Length plugin. Upon activation, simply go to Settings » Control Comment Length to configure the plugin settings.

您需要做的第一件事是安装并激活Control Comment Length插件。 激活后,只需转到设置»控制注释长度即可配置插件设置。

Controlling comment length in WordPress using a plugin

The plugin’s user interface is in German with English. You can set both minimum and maximum number of characters a comment can have. We recommend using 60 for minimum and 5000 for maximum character count.

插件的用户界面为德语和英语。 您可以设置注释可以包含的最小和最大字符数。 我们建议最小字符数为60,最大字符数为5000。

You can also add messages that will be visible to users when there comments are either too short or too long. The plugin only provides these messages in the German language. You can replace it with your own message.

您还可以添加在评论太短或太长时用户可见的消息。 该插件仅以德语提供这些消息。 您可以用自己的消息替换它。

方法2:使用代码段限制注释长度 (Method 2: Limit Comment Length Using Code Snippet)

The second method is for users who don’t mind dealing with code code. We will add a filter hook to preprocess_comment. This filter is run before WordPress saves any comments to database or runs any other pre-processing on submitted comments. We will use it to check the comment length. If it is above or below the set comment length parameters, then we will show users an error message.

第二种方法适用于不介意处理代码的用户。 我们将过滤器挂钩添加到preprocess_comment 。 在WordPress将任何评论保存到数据库或对提交的评论运行任何其他预处理之前,将运行此过滤器。 我们将使用它来检查评论长度。 如果它位于设置的注释长度参数之上或之下,那么我们将向用户显示一条错误消息。

Simply add this code to your theme’s functions.php file or a site-specific plugin.

只需将此代码添加到主题的functions.php文件或特定站点的插件中即可


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

Comment too long error

We hope this article helped you limit comment length in WordPress. You may also want to checkout our guide on 12 vital tips and tools to combat comment spam in WordPress.

我们希望本文能帮助您限制WordPress中的注释长度。 您可能还需要查看有关12个重要提示和工具的指南,以打击WordPress中的垃圾评论

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-limit-comment-length-in-wordpress/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值