今天有位小伙伴在群中询问 WordPress 新用户注册显示密码的问题,由于 WordPress 默认的是不让用户自己去填写密码的,而是系统自动给用户生成一个密码并且发送到用户邮箱,相对来说可能有些用户会不习惯,今天就来教大家优化 WordPress 的用户注册体验,让用户自己设置账户密码,其实很简单只需要在主题的 function.php 加上以下代码:

  1. <?php
  2. function ztmao_show_register(){
  3.     <p>
  4. for="password"> 密码:<br/>
  5.         <input id="password" class="input" type="password" tabindex="30" size="25" value="" name="password" />
  6.     </p>
  7.         <label for="repeat_password"> 确认密码<br/>
  8. "repeat_password" class="input" type="password" tabindex="40" size="25" value="" name="repeat_password" />
  9.         </label>
  10.     <p>
  11. for="are_you_human" style="font-size:11px"> 挖掘机技术哪家强?(蓝翔)<br/>
  12.         <input id="are_you_human" class="input" type="text" tabindex="40" size="25" value="" name="are_you_human" />
  13.     </p>
  14. }
  15. function ts_check_extra_register_fields($login, $email, $errors) {
  16. if ( $_POST['password'] !== $_POST['repeat_password'] ) {
  17.         $errors->add( 'passwords_not_matched', "<strong>ERROR</strong>: 两次密码不一致" );
  18.     if ( strlen( $_POST['password'] ) < 8 ) {
  19. $errors->add( 'password_too_short', "<strong>ERROR</strong>: 密码长度小于 8 位!" );
  20.     }
  21. if ( $_POST['are_you_human'] !== '蓝翔' ) {
  22.         $errors->add( 'not_human', "<strong>ERROR</strong>: 回答错误,请重新填写注册信息!" );
  23. }

为了保证不被注册机骚扰此代码中还自带了一个验证问题字段,防止注册机批量注册垃圾用户。虽然让用户可以自己填写密码,但是有些用户更加喜欢让系统为他生成密码,为了给这些用户提供方便,我们可以判断下当前用户注册时是否填了密码,如果没填再让系统生成一个,代码如下:

  1. add_action( 'user_register', 'ztmao_register_extra_pass', 100 );
  2. function ztmao_register_extra_pass( $user_id, $password="", $meta=array() ){
  3.     $userdata = array();
  4. $userdata['ID'] = $user_id;
  5. if( $_POST['password'] ){
  6. $userdata['user_pass'] = $_POST['password'];
  7. }
  8. $userdata);
  9. }
    1. add_filter( 'gettext', 'v7v3_edit_text' );
    2. function ztmao_edit_text( $text ) {
    3.     if ( $text == 'A password will be e-mailed to you.' ) {
    4. $text = '如果您不填写密码,系统将为您生成一个密码, 并发送至您的邮箱。';
    5.     }
    6. return $text;
    7. }