本页目录
为了伤害那些在评论区乱打一通的人,虽然在国内用的少吧,可以选择性的加上去。
在主题根目录下的functions.php文件中的<?php下添加以下代码并保存。
/*禁止纯英文、纯日文评论 * https://www.npc.ink/18129.html * */function refused_english_comments($incoming_comment) { $pattern = '/[一-龥]/u'; // 禁止全英文评论 if(!preg_match($pattern, $incoming_comment['comment_content'])) { wp_die( "您的评论中必须包含汉字!" ); } $pattern = '/[あ-んア-ン]/u'; // 禁止日文评论 if(preg_match($pattern, $incoming_comment['comment_content'])) { wp_die( "评论禁止包含日文!" ); } return( $incoming_comment );}add_filter('preprocess_comment', 'refused_english_comments');实战
- 优化正则
- 添加返回按钮,避免用户误操作
public static function refused_english_comments($incoming_comment) { $pattern = '/[p{Script=Han}]/u'; if (!preg_match($pattern, $incoming_comment['comment_content'])) { $message = '您的评论中必须包含汉字!'; $message .= '<br/><a href="#" onclick="history.back();"> <button class="button" style="margin: 1em 0;">返回</button> </a>'; wp_die($message); } return $incoming_comment; }