下面是插件代码,自行取走。
/*
Plugin Name: 简单算术题评论验证码插件
Description: 提交评论之前必须写出简单的算术题答案
Version: 1.0
Author: 否子戈
*/
代码如下 |
复制代码 |
if(!class_exists('comment_capatcha')) {
class comment_capatcha {
function __construct() {
add_action('comment_form', array(& $this, 'print_capatcha'));
add_filter('preprocess_comment', array(& $this, 'preprocess_comment'));
}
function print_capatcha() {
if(!is_user_logged_in()) {
global $post;
session_start();
$rand_1 = mt_rand(1,20);
$rand_2 = mt_rand(1,20);
$_SESSION['capatcha_'.$post->ID] = $rand_1 + $rand_2;
$str = ' ';
echo $str;-
}
}
function preprocess_comment($commentdata) {
if(!is_user_logged_in()) {
session_start();
$post_id = isset($_POST['comment_post_ID']) ? $_POST['comment_post_ID'] : 0;
if(!$post_id){
wp_die('警告:数据来源非法!!');
}
$capatcha = $_SESSION['capatcha_'.$post_id];
if($capatcha != $_POST['capatcha']){
wp_die( __('警告:你智商有问题,不允许评论,请返回重新计算。') );
}
unset($_SESSION['capatcha_'.$post_id]);
}
return $commentdata;
}
}
}
if( !isset($comment_capatcha) ) {
$comment_capatcha =& new comment_capatcha();
}
|
将上面的代码保存在comment_capatcha.php中,上传到wordpress的wp-content/plugins/目录下,到后台启动该插件就可以了。
不过,如果你是通过阅读如何自己设计wordpress评论列表及评论框之后自己设计的自己的评论框,那么一定要注意do_action('comment_form', $post->ID);的位置,因为验证算术题会在它的位置打印出来