拒绝全英文垃圾评论!仅用代码实现

时间:2022-04-23
本文章向大家介绍拒绝全英文垃圾评论!仅用代码实现,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

从无人问津到略有评论,再到垃圾评论的“空袭”,这大概是每个wordpress博主都会经历的事。对付垃圾评论,一般人通常是安装wordpress官方的Akismet插件。

但是你应该发现,这些垃圾评论绝大部分是全英文的评论;再说到Akismet插件的误伤率是在是太大,且耗服务器资源。其实我们只要屏蔽全英文的评论就可以了(毕竟你个中文博客还有老外成为你的读者?),而这在实现方面是在是简单,又是添加几行代码就行了。减少一插件,何乐而不为?

下面Jeff就为大家带来无插件仅代码实现拒绝全英文垃圾评论的方法。

打开主题目录下的funtions.php,就在最后一个 ?>加入如下的代码:

<!--?php <br ?--> // 禁止全英文评论
add_theme_support('automatic-feed-links'); // Add default posts and comments RSS feed links to head
function scp_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "You should type some Chinese word (like "你好") in your comment to pass the spam-check, thanks for your patience! 您的评论中必须包含汉字!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'scp_comment_post');
?>

如果使用的是Ajax 评论,需要将wp_die 改为 err ,不然页面会有错位现象。

保存后就完成了。试着输入全英文评论,你看看显示什么?

效果如下面:

You should type some Chinese word (like ”你好”) in your comment to pass the spam-check, thanks for your patience! 您的评论中必须包含汉字!

哈哈!又一个插件宣告“退役”!