今天有位小伙伴在群中詢問wordpress新用戶注冊顯示密碼的問題,由于wordpress默認的是不讓用戶自己去填寫密碼的,而是系統自動給用戶生成一個密碼并且發送到用戶郵箱,相對來說可能有些用戶會不習慣,今天A5小編輯就來教大家優化wordpress的用戶注冊體驗,讓用戶自己設置賬戶密碼,其實很簡單只需要在主題的function.php加上以下代碼:
<?php
add_action( 'register_form', 'v7v3_show_register' );
function v7v3_show_register(){
?>
<p>
<label for="password">密碼:<br/>
<input id="password" class="input" type="password" tabindex="30" size="25" value="" name="password" />
</label>
</p>
<p>
<label for="repeat_password">確認密碼<br/>
<input id="repeat_password" class="input" type="password" tabindex="40" size="25" value="" name="repeat_password" />
</label>
</p>
<p>
<label 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" />
</label>
</p>
<?php
}
add_action( 'register_post', 'ts_check_extra_register_fields', 10, 3 );
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', 'v7v3_register_extra_pass', 100 );
function v7v3_register_extra_pass( $user_id ){
$userdata = array();
$userdata['ID'] = $user_id;
if ( $_POST['password'] !== '' ) {
$userdata['user_pass'] = $_POST['password'];
}
$new_user_id = wp_update_user( $userdata );
}
當然為了給用戶更好的體驗,我們可以在注冊框下方加個提示,代碼如下:
add_filter( 'gettext', 'v7v3_edit_text' );
function v7v3_edit_text( $text ) {
if ( $text == 'A password will be e-mailed to you.' ) {
$text = '如果您不填寫密碼,系統將為您生成一個密碼, 并發送至您的郵箱。';
}
return $text;
}
新聞熱點
疑難解答