今天有位小夥伴在群中詢問 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. }