WordPress教程

优化用户注册体验(wordpress 自定义密码)

阿里云

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

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

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

  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.     wp_update_user($userdata);  
  9. }  
  10.  
  11. /*如果强制用户输入密码,则可将此段代码的注释去除 
  12. add_action('admin_init', 'remove_default_password_nag'); 
  13. function remove_default_password_nag() { 
  14.     global $user_ID; 
  15.     delete_user_setting('default_password_nag', $user_ID); 
  16.     update_user_option($user_ID, 'default_password_nag', false, true); 
  17. } 
  18. */

当然为了给用户更好的体验,我们可以在注册框下方加个提示,代码如下:

  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. }

优化用户注册体验(wordpress 自定义密码)

已有 796 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!