介绍
最近都能看到垃圾评论,纯英文有些还附带网址,现在每天几条虽然不多,但以后很难说。每次都要到后台删也是很麻烦的,又不喜欢装插件,就给子比加个正则验证吧,限制评论纯数字/纯英文/纯表情

教程
将代码放到/wp-content/themes/zibll/func.php
文件即可。注意 此方法评论必须带中文 禁止了纯数字、纯字母和纯表情。
//屏蔽纯英文评论function refused_english_comments($incoming_comment) {// 获取评论内容$comment_content = $incoming_comment['comment_content'];// 去除评论内容中的 [g=xxx] xxx为任意字符串$comment_content = preg_replace('/\[g=[^\]]*\]/', '', $comment_content);// 检查评论内容是否为空if (empty($comment_content)) {wp_die('{"error":1,"ys":"danger","msg":"评论不能是纯表情内容 <br/>Comments cannot be purely emoji content"}');}// 检查评论内容是否包含中文$pattern = '/[一-龥]/u';// 禁止全英文评论if (!preg_match($pattern, $comment_content)) {wp_die('{"error":1,"ys":"danger","msg":"评论必须包括中文 <br/>Comments must include Chinese"}');}return $incoming_comment;}add_filter('preprocess_comment', 'refused_english_comments');//屏蔽纯英文评论 function refused_english_comments($incoming_comment) { // 获取评论内容 $comment_content = $incoming_comment['comment_content']; // 去除评论内容中的 [g=xxx] xxx为任意字符串 $comment_content = preg_replace('/\[g=[^\]]*\]/', '', $comment_content); // 检查评论内容是否为空 if (empty($comment_content)) { wp_die('{"error":1,"ys":"danger","msg":"评论不能是纯表情内容 <br/>Comments cannot be purely emoji content"}'); } // 检查评论内容是否包含中文 $pattern = '/[一-龥]/u'; // 禁止全英文评论 if (!preg_match($pattern, $comment_content)) { wp_die('{"error":1,"ys":"danger","msg":"评论必须包括中文 <br/>Comments must include Chinese"}'); } return $incoming_comment; } add_filter('preprocess_comment', 'refused_english_comments');//屏蔽纯英文评论 function refused_english_comments($incoming_comment) { // 获取评论内容 $comment_content = $incoming_comment['comment_content']; // 去除评论内容中的 [g=xxx] xxx为任意字符串 $comment_content = preg_replace('/\[g=[^\]]*\]/', '', $comment_content); // 检查评论内容是否为空 if (empty($comment_content)) { wp_die('{"error":1,"ys":"danger","msg":"评论不能是纯表情内容 <br/>Comments cannot be purely emoji content"}'); } // 检查评论内容是否包含中文 $pattern = '/[一-龥]/u'; // 禁止全英文评论 if (!preg_match($pattern, $comment_content)) { wp_die('{"error":1,"ys":"danger","msg":"评论必须包括中文 <br/>Comments must include Chinese"}'); } return $incoming_comment; } add_filter('preprocess_comment', 'refused_english_comments');
暂无评论内容