一个不错的WordPress评论算术题验证码插件
算术题验证码我们见过最多的就是如1+3等于多少,下面我来给各位介绍在WordPress评论中加个一个算术题验证码功能,各位可参考消息.
下面是插件代码,自行取走:
-
-
-
-
-
-
- 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;-
- }
- }
- 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);的位置,因为验证算术题会在它的位置打印出来.