/**
* WordPress 注册表单添加验证问题 (支持多个随机问题)
* http://www.xhsay.com
*/
functionrand_reg_question(){
$register_number=rand(0,1);// 设置随机数的返回范围
$_SESSION['register_number']=$register_number;
}
add_action('login_head','rand_reg_question');
global$register_questions;
global$register_answers;
// 添加问题数组
$register_questions=array('中国的首都在哪里?','Google 是哪个国家的公司?');
// 添加答案数组 (与上面的问题对应)
$register_answers=array('北京','美国');
add_action('register_form','add_security_question');
functionadd_security_question(){
global$register_questions;
$register_number=$_SESSION['register_number'];
?>
<p>
<label><?phpecho$register_questions[$register_number];?><br/>
<input type="text"name="user_proof"id="user_proof"class="input"size="25"tabindex="20"/>
</label>
</p>
<?php}
add_action('register_post','add_security_question_validate',10,3);
functionadd_security_question_validate($sanitized_user_login,$user_email,$errors){
global$register_answers;
$register_number=$_SESSION['register_number'];
if(!isset($_POST['user_proof'])||empty($_POST['user_proof'])){
return$errors->add('proofempty','<strong> 错误</strong>: 您还没有回答问题。');
}elseif(strtolower($_POST['user_proof'])!=$register_answers[$register_number]){
return$errors->add('prooffail','<strong> 错误</strong>: 您的回答不正确。');
}
}