久青草国产观看在线视频,在线观看欧美日女,777毛片,亚洲国产精品99久久久久久久

wordpress開發教程:優化用戶注冊體驗(自定義密碼)

2015-04-03 wordpress教程
  • 文章介紹
  • 快速入門
  • 評價&建議

今天有位小伙伴在群中詢問wordpress新用戶注冊顯示密碼的問題,由于wordpress默認的是不讓用戶自己去填寫密碼的,而是系統自動給用戶生成一個密碼并且發送到用戶郵箱,相對來說可能有些用戶會不習慣,今天就來教大家優化wordpress的用戶注冊體驗,讓用戶自己設置賬戶密碼,其實很簡單只需要在主題的function.php加上以下代碼:

  1. <?php  
  2. add_action( 'register_form', 'v7v3_show_register' );  
  3. function ztmao_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. }  
0 0

企業建站推薦正版商業主題,國內專業團隊開發,完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計幫助1300+客戶成功建站,為站長提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務熱線

wordpress建站咨詢