加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

优化用户注册体验(wordpress 自定义密码)

发布时间:2020-12-14 14:31:28 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 今天有位小伙伴在群中询问 wordpress 新用户注册显示密码的问题,由于 wordpress 默认的是不让用户自己去填写密码的,而是系统自动给用户生成一个密码

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

今天有位小伙伴在群中询问 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" 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;

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读