Home  >  Article  >  CMS Tutorial  >  Solve the problem of mass spam comments in WordPress

Solve the problem of mass spam comments in WordPress

藏色散人
藏色散人forward
2020-05-11 13:56:113199browse

The following column WordPress Website Building Tutorial will introduce to you how to solve the problem of spam comments being sent in groups in WordPress. I hope it will be helpful to friends who need it!

Solve the problem of mass spam comments in WordPress

Many of our netizens have reported that during the process of building a WordPress website, they found that there are a lot of spam comments in the background comments every day, and it seems that these comments are not sent manually. Indeed, we may encounter a lot of spam comments sent by software. For example, we may see a lot of English messages. This information will increase our daily deletion time cost, and it is also not good for the website. Is there any way we can solve this problem?

Our most likely method is to add comment verification. You need to enter a verification code or other verification mechanisms when commenting. These are also good methods. Today, what we need to introduce in our WordPress class is how to block these comment contents through two pieces of code.

First, the comment content needs to be in Chinese

What we see in the comments are all in English or other languages, not Chinese. Then we must comment in Chinese.

// 评论中需要有中文 cnwper.com
function wp_refused_spam_comments($comment_data) {
$pattern = '/[一-龥]/u';
$jpattern = '/[ぁ-ん]+|[ァ-ヴ]+/u';
if (!preg_match($pattern, $comment_data['comment_content'])) {
err(__('评论中需要有一个汉字!'));
}
if (preg_match($jpattern, $comment_data['comment_content'])) {
err(__('不能有日文!'));
}
return ($comment_data);
}
add_filter('preprocess_comment', 'wp_refused_spam_comments');

Second, prohibit links in comments

Many people leave comments on blogs to promote the website. If we prohibit links, not many people will leave comments.

//禁止发链接 cnwper.com
function wp_comment_post( $incoming_comment ) {
$http = &#39;/[href="|rel="nofollow"|http://|</a>]/u&#39;;
if(preg_match($http, $incoming_comment[&#39;comment_content&#39;])) {
err( "禁止发链接地址!" );
}
return( $incoming_comment );
}
add_filter(&#39;preprocess_comment&#39;, &#39;wp_comment_post&#39;);

It’s as simple as adding two pieces of code to the Functions.php file in your current WordPress theme.

For more WordPress technical articles, please visit the WordPress Tips column!

The above is the detailed content of Solve the problem of mass spam comments in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete