首先添加到主题的functions.php
文件内,效果见本站~~
评论算数验证码:
//评论算数 function spam_protection_math() { $num1 = rand(1, 9); $num2 = rand(1, 9); echo "<span class='yanzheng'> {$num1} + {$num2} = <input type='text' style='width:60px;' name='sum' class='math_textfield' value=''>" . "<input type='hidden' name='num1' value='{$num1}'>" . "<input type='hidden' name='num2' value='{$num2}'>" . "</span>"; } function spam_protection_pre($commentdata) { $sum = $_POST['sum']; switch ($sum) { case $_POST['num1'] + $_POST['num2']: break; case null: err('请输入计算结果!'); break; default: err('老铁数学不行啊!再算算!'); } return $commentdata; } if ($comment_data['comment_type'] == '') { add_filter('preprocess_comment', 'spam_protection_pre'); }
敏感词和限制只能中文评论:
//评论敏感词和中文 function uedsc_fuckspam($comment) { if (is_super_admin()) { return $comment; } if (wp_blacklist_check($comment['comment_author'], $comment['comment_author_email'], $comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'])) { header("Content-type: text/html; charset=utf-8"); err('您的评论内容包含敏感词汇,请检查内容后再次提交!'); } else { return $comment; } } add_filter('preprocess_comment', 'uedsc_fuckspam'); function refused_spam_comments($comment_data) { $pattern = '/[一-龥]/u'; if (!preg_match($pattern, $comment_data['comment_content'])) { err('评论内容必须含中文!'); } return $comment_data; } add_filter('preprocess_comment', 'refused_spam_comments');
下面的代码用于输出调用,添加到评论文件的合适位置,一般添加在button、submit提交按钮之前。我的是在comments.php
文件。
<?php echo spam_protection_math(); ?>
评论0