乌徒帮技术范WordPressWordPress插件 › WordPress评论算术题验证码插件,智商合格才能评论

WordPress评论算术题验证码插件,智商合格才能评论

分类:WordPress插件,成品展售

我在阅读别人的博客的时候,发现有的博客使用了图片验证码来让评论者填写正确的验证码才能提交评论,我将之前用过的一个图片评论验证码插件用在博客上时,却发现无法制作图片,于是我想只能使用文字了所以打算做这个简单的算术题验证码插件。好了,废话不多,下面是插件代码,自行取走。

/*
 Plugin Name: 简单算术题评论验证码插件
 Plugin URI:  http://www.utubon.com/wordpress-plugin-comment-capatcha-figure/
 Description: 提交评论之前必须写出简单的算术题答案
 Version: 1.0
 Author: 否子戈
 Author URI: http://www.utubon.com
*/

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? = '<div id="capatcha-area"><label>';
??????????????? $str .= "{$rand_1} + {$rand_2} = ".'<input type="text" name="capatcha" id="capatcha" />';
??????????????? $str .= '智商合格才能评论 *';
??????????????? $str .= '</label></div>';

??????????????? echo $str;[ad width=300 height=250 float=right]
??????????? }
??????? }
??????? 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);的位置,因为验证算术题会在它的位置打印出来。

填写个人信息,赶快回复吧!