wordpress非插件防垃圾评论方法
这个方法来自网上, 已经被转载多次了, 很多地方叫 "WordPress 代码添加算术验证码防止垃圾评论", 我已经无法找到原始出处. 我只是对它进行了一点小小的改进, 把它变成了 +-/* 四则运算. 方法也不是很复杂, 加入一个运算法则的变量就行了. 效果见下面的评论.
在主题文件夹里找到comments.php文件,在你想要显示输入框的地方加入代码(我是填写在:div class="submitbutton"中):
<?php
$b = rand(1,9);
$c = rand(1,4);
if($c==1){$a = rand(0,40); $s = '+';}
elseif($c==2){$a = rand(0,30)+$b; $s = '-';}
elseif($c==3){$a = rand(0,5); $s = '*';}
else {$a = $b*$c; $s = '/';}
echo _e("$a $s $b =", 'inove'); ?>
<input type="text" name="cal" id="cal" class="textfield" value="" size="4" tabindex="5" style="width:20px" /><?php if ($req) _e('(required)', 'inove'); ?>
<input name="a" type="hidden" value="<?php echo $a;?>" />
<input name="b" type="hidden" value="<?php echo $b;?>" />
<input name="c" type="hidden" value="<?php echo $c;?>" />
在文件wp-comments-post.php中加入如下代码进行验证(我是填写在:if ( '' == $comment_content )上):
if ( !$user->ID ) {
$a = trim($_POST['a']);
$b = trim($_POST['b']);
$c = trim($_POST['c']);
$sub = trim($_POST['cal']);
if($c==1) $res = (bool)($sub == $a + $b);
elseif($c==2) $res = (bool)($sub == $a - $b);
elseif($c==3) $res = (bool)($sub == $a * $b);
elseif($c==4) $res = (bool)($sub == $a / $b);
else $res = false;
if(!$res || empty($sub)){
wp_die( __('Error: please enter the right calculating result.') );
}
}
这篇文章来自 迷途知返
我一直喜欢wp插件,模块化多好
最麻烦的就是改代码
@alswl 呵呵。跟我另一个朋友说的正好相反。他恨不得所有插件都代码化。我这个也是他发给我看的。正好升级2.9,我顺道就弄我的BLOG里。
@ a18ccms
我现在博客又无法访问了
正在联系com域名和美国空间
@alswl
。。。。咋又访问不了了。。汗~~
呵呵,不错,对于机器留言很有效~~
@笑话 呵呵~~~