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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”>挖掘机技术哪家强?(蓝翔)<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;

}

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部