今天有位小夥伴在群中詢問 WordPress 新使用者註冊顯示密碼的問題,由於 WordPress 預設的是不讓使用者自己去填寫密碼的,而是系統自動給使用者生成一個密碼並且傳送到使用者郵箱,相對來說可能有些使用者會不習慣,今天就來教大家最佳化 WordPress 的使用者註冊體驗,讓使用者自己設定賬戶密碼,其實很簡單隻需要在主題的 function.php 加上以下程式碼:
- <?php
- function ztmao_show_register(){
- <p>
- for="password"> 密碼:<br/>
- <input id="password" class="input" type="password" tabindex="30" size="25" value="" name="password" />
- </p>
- <label for="repeat_password"> 確認密碼<br/>
- "repeat_password" class="input" type="password" tabindex="40" size="25" value="" name="repeat_password" />
- </label>
- <p>
- for="are_you_human" style="font-size:11px"> 挖掘機技術哪家強?(藍翔)<br/>
- <input id="are_you_human" class="input" type="text" tabindex="40" size="25" value="" name="are_you_human" />
- </p>
- }
- function ts_check_extra_register_fields($login, $email, $errors) {
- if ( $_POST['password'] !== $_POST['repeat_password'] ) {
- $errors->add( 'passwords_not_matched', "<strong>ERROR</strong>: 兩次密碼不一致" );
- if ( strlen( $_POST['password'] ) < 8 ) {
- $errors->add( 'password_too_short', "<strong>ERROR</strong>: 密碼長度小於 8 位!" );
- }
- if ( $_POST['are_you_human'] !== '藍翔' ) {
- $errors->add( 'not_human', "<strong>ERROR</strong>: 回答錯誤,請重新填寫註冊資訊!" );
- }
為了保證不被序號產生器騷擾此程式碼中還自帶了一個驗證問題欄位,防止序號產生器批次註冊垃圾使用者。雖然讓使用者可以自己填寫密碼,但是有些使用者更加喜歡讓系統為他生成密碼,為了給這些使用者提供方便,我們可以判斷下當前使用者註冊時是否填了密碼,如果沒填再讓系統生成一個,程式碼如下:
- add_action( 'user_register', 'ztmao_register_extra_pass', 100 );
- function ztmao_register_extra_pass( $user_id, $password="", $meta=array() ){
- $userdata = array();
- $userdata['ID'] = $user_id;
- if( $_POST['password'] ){
- $userdata['user_pass'] = $_POST['password'];
- }
- $userdata);
- }
-
- add_filter( 'gettext', 'v7v3_edit_text' );
- function ztmao_edit_text( $text ) {
- if ( $text == 'A password will be e-mailed to you.' ) {
- $text = '如果您不填寫密碼,系統將為您生成一個密碼, 併傳送至您的郵箱。';
- }
- return $text;
- }