WordPress 自定义密码优化用户注册体验

WordPress 自定义密码优化用户注册体验

作者 : 资源客 发布时间: 2019-12-25

今天有位小伙伴在群中询问 wordpress 新用户注册显示密码的问题,由于 wordpress 默认的是不让用户自己去填写密码的,而是系统自动给用户生成一个密码并且发送到用户邮箱,相对来说可能有些用户会不习惯,今天就来教大家优化 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', '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'];  
}  
    wp_update_user($userdata);  
}  
 
/*如果强制用户输入密码,则可将此段代码的注释去除 
add_action('admin_init', 'remove_default_password_nag'); 
function remove_default_password_nag() { 
    global $user_ID; 
    delete_user_setting('default_password_nag', $user_ID); 
    update_user_option($user_ID, 'default_password_nag', false, true); 
} 
*/

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

add_filter( 'gettext', 'v7v3_edit_text' );  
function ztmao_edit_text( $text ) {  
    if ( $text == 'A password will be e-mailed to you.' ) {  
        $text = '如果您不填写密码,系统将为您生成一个密码, 并发送至您的邮箱。';  
    }  
    return $text;  
}

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress 自定义密码优化用户注册体验