算術題驗證碼我們見過最多的就是如1+3等于多少,下面我來給各位介紹在WordPress評論中加個一個算術題驗證碼功能,各位可參考消息.
下面是插件代碼,自行取走:
- /*
- 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 = '<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);的位置,因為驗證算術題會在它的位置打印出來.
新聞熱點
疑難解答
圖片精選