这段代码可以屏蔽纯英文/纯表情/相同评󠄐󠄹󠅀󠄪󠄣󠄞󠄡󠄣󠄥󠄞󠄢󠄤󠄦󠄞󠄢󠄨󠄬󠅒󠅢󠄟󠄮󠄐󠅅󠄹󠄴󠄪󠄾󠅟󠅤󠄐󠄼󠅟󠅗󠅙󠅞󠄬󠅒󠅢󠄟󠄮󠅄󠅙󠅝󠅕󠄪󠄡󠄧󠄤󠄢󠄩󠄢󠄩󠄢󠄠󠄤󠄬󠅒󠅢󠄟󠄮󠇕󠅰󠆖󠇖󠅴󠅿󠇕󠅽󠆊󠇕󠆞󠆒󠄐󠇗󠅹󠅸󠇖󠆍󠅳󠇖󠅹󠅰󠇖󠆌󠅹󠄬󠅒󠅢󠄟󠄮论。
注释已经全部写上,有󠄐󠄹󠅀󠄪󠄣󠄞󠄡󠄣󠄥󠄞󠄢󠄤󠄦󠄞󠄢󠄨󠄬󠅒󠅢󠄟󠄮󠄐󠅅󠄹󠄴󠄪󠄾󠅟󠅤󠄐󠄼󠅟󠅗󠅙󠅞󠄬󠅒󠅢󠄟󠄮󠅄󠅙󠅝󠅕󠄪󠄡󠄧󠄤󠄢󠄩󠄢󠄩󠄢󠄠󠄤󠄬󠅒󠅢󠄟󠄮󠇕󠅰󠆖󠇖󠅴󠅿󠇕󠅽󠆊󠇕󠆞󠆒󠄐󠇗󠅹󠅸󠇖󠆍󠅳󠇖󠅹󠅰󠇖󠆌󠅹󠄬󠅒󠅢󠄟󠄮能力的小伙伴可以考虑去二次开发!
有用的回复一下,让我󠄐󠄹󠅀󠄪󠄣󠄞󠄡󠄣󠄥󠄞󠄢󠄤󠄦󠄞󠄢󠄨󠄬󠅒󠅢󠄟󠄮󠄐󠅅󠄹󠄴󠄪󠄾󠅟󠅤󠄐󠄼󠅟󠅗󠅙󠅞󠄬󠅒󠅢󠄟󠄮󠅄󠅙󠅝󠅕󠄪󠄡󠄧󠄤󠄢󠄩󠄢󠄩󠄢󠄠󠄤󠄬󠅒󠅢󠄟󠄮󠇕󠅰󠆖󠇖󠅴󠅿󠇕󠅽󠆊󠇕󠆞󠆒󠄐󠇗󠅹󠅸󠇖󠆍󠅳󠇖󠅹󠅰󠇖󠆌󠅹󠄬󠅒󠅢󠄟󠄮知道~
示意图:
![图片[1]-子比主题 屏蔽纯英文/纯表情/相同评论-倦意博客](https://jyblog.cn/wp-content/uploads/2025/03/20250324181529117-QQ20250324-181500.png)
![图片[2]-子比主题 屏蔽纯英文/纯表情/相同评论-倦意博客](https://jyblog.cn/wp-content/uploads/2025/03/20250324180504337-QQ20250324-180457.png)
代码:
//屏蔽纯英文评论
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 check_duplicate_comment($commentdata) {
// 获取当前帖子的ID
$post_id = $commentdata['comment_post_ID'];
// 获取当前帖子的所有评论
$comments = get_comments(array(
'post_id' => $post_id,
'status' => 'approve' // 只获取已审核的评论
));
// 获取新评论内容
$new_comment_content = $commentdata['comment_content'];
// 标记是否发现重复评论
$duplicate_found = false;
// 遍历同一帖子的所有评论
foreach ($comments as $comment) {
// 获取已存在评论的内容
$existing_comment_content = $comment->comment_content;
// 检查新评论内容是否与已存在评论相同
if ($new_comment_content == $existing_comment_content) {
// 如果有相同评论内容的评论存在,标记为发现重复评论
$duplicate_found = true;
break;
}
}
// 如果发现重复评论,显示错误消息并终止评论发布
if ($duplicate_found) {
wp_die('{"error":1,"ys":"danger","msg":"同一文章内禁止发布相同内容的评论 <br/>Dont repeat the same comments in one post"}');
}
return $commentdata;
}
// 添加钩子,使函数在评论提交前运行
add_filter('preprocess_comment', 'check_duplicate_comment');
如果想管理员跳过检测,可以增加以下代码在两个function内:
// 检查评论发布者是否是管理员
$user = wp_get_current_user();
if (in_array('administrator', $user->roles)) {
// 如果评论发布者是管理员,直接返回评论数据,不进行重复评论检查
return $commentdata;
}
© 版权声明
THE END
暂无评论内容