分类
WP折腾

为WP添加简单注册验证

为了防止恶意注册, 我们需要注册用户回答简单的问题。例如下面的例子中, 我们要求用户回答本站名称。

直接将下列代码放到你的functions.php

//add verification on register page
function register_code(){ ?>
  <p>
    <label for="vercode"><?php _e("请输入本站名称: <strong>VanRaspi</strong>", "userfun"); ?><br />
    <input id="vercode" class="input" size="25" type="text" name="vercode" /></label>
    </p>
    <?php
}
//add validation
function register_code_validate($sanitized_user_login, $user_email, $errors){
  $vercode=$_POST['vercode'];
  //haven't input
  if (!isset($vercode) || $vercode==''){
     return $errors->add('vercode_error', __("<strong>错误 : </strong>请输入本站名称!","userfun"));
  }elseif(strtolower($vercode)!='vanraspi'){
    //if the question is not right
     return $errors->add('vercode_fail', __("<strong>错误 : </strong>你输入的站点名不正确!", "userfun"));
  }
}

add_action('register_form', 'register_code');
add_action('register_post', 'register_code_validate', 10, 3);

参考链接

  1. register_form
  2. registration_errors

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据