wordpress用户注册界面直接填写用户名密码注册账号
需求整理来源
今天纪实阁在外出的时候,有一个QQ好友咨询说可不可以用户注册的时候不需要通过邮件,可以直接设置注册用户名时候的密码呢,回来了想了下这样子的操作在后期管理维护中,定然是有利亦有弊,但限于既然有这个需求,作为东莞网站建设培训的指导人员,纪实阁还是通过修改、调试整理出wordpress账号密码注册需求的修改方案;
WordPress注册时,只需要填写用户名和电子邮箱,点击提交后密码由系统产生并通过邮件发给用户,用户用随机密码登陆后要更改密码。你是否觉得WordPress的注册有点绕弯的感觉,既然用户注册后还要将随机生成的密码更改,为什么不让他们自己填写密码呢?纪实阁想主要的原因可能是——防止机器人注册,验证码对机器人并不是十分有效,甚至谷歌那种晦涩难懂的验证码也能被某些机器人识别,但机器人没法接收邮件,所以这种方式可以有效的防止spam。
需求修改方案
通过register_form action向注册表单添加密码、重复密码和防机器人的验证输入框,防止机器人注册的方法是要求用户填写要注册的网站的名称,这个方法很棒,比验证码方便的多。
将以下涉及到的这些代码可以放到主题的functions.php中即可:
<?php // Add Password, Repeat Password and Are You Human fields to WordPress registration form add_action( 'register_form', 'ts_show_extra_register_fields' ); function ts_show_extra_register_fields(){ ?> <p> <label for="password">Password<br/> <input id="password" class="input" type="password" tabindex="30" size="25" value="" name="password" /> </label> </p> <p> <label for="repeat_password">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">Sorry, but we must check if you are human. What is the name of website you are registering for?<br/> <input id="are_you_human" class="input" type="text" tabindex="40" size="25" value="" name="are_you_human" /> </label> </p> <?php }
检查用户的输入,两次输入的密码是否一致,是否正确填写网站名称:
// Check the form for errors 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>: Passwords must match" ); } if ( strlen( $_POST['password'] ) < 8 ) { $errors->add( 'password_too_short', "<strong>ERROR</strong>: Passwords must be at least eight characters long" ); } if ( $_POST['are_you_human'] !== get_bloginfo( 'name' ) ) { $errors->add( 'not_human', "<strong>ERROR</strong>: Your name is Bot? James Bot? Check bellow the form, there's a Back to [sitename] link." ); } }
存储用户输入的密码,如果用户没有填写密码,什么也不做,让WordPress自动生成密码:
// Storing WordPress user-selected password into database on registration add_action( 'user_register', 'ts_register_extra_fields', 100 ); function ts_register_extra_fields( $user_id ){ $userdata = array(); $userdata['ID'] = $user_id; if ( $_POST['password'] !== '' ) { $userdata['user_pass'] = $_POST['password']; } $new_user_id = wp_update_user( $userdata ); }
最后就要处理那句不协调的“A password will be e-mailed to you”,通过gettext filter处理一下即可:
add_filter( 'gettext', 'ts_edit_password_email_text' ); function ts_edit_password_email_text ( $text ) { if ( $text == 'A password will be e-mailed to you.' ) { $text = 'If you leave password fields empty one will be generated for you. Password must be at least eight characters long.'; } return $text; }
这些代码可以放到主题的functions.php中,但不推荐这样,其实做成插件和放到functions.php中在性能上没有很大区别,但functions.php的可移植性和方便性会大打折扣,建议做成插件。
要做成插件,只要把这些代码拷贝到一个文件中,并写上标准插件声明就可以了。
修改后效果如下:
标准插件声明格式
假如上方代码保存为文件名称为:zhuce.php,则在此文件打开后第一个“<?php”后面添加以下代码:
/* Plugin Name: 用户注册变更直接填写密码 Plugin URI: https://www.diebaosoft.com/3723.html Description: WordPress注册界面:用户填写密码 Version: 4.0 Author: 纪实阁 Author URI: http://www.diebaosoft.com */ //首先头部加载样式
1、本文由 ♚纪实阁付涛♚ 作者:付涛纪实阁 发表,其版权均为 ♚纪实阁付涛♚ 所有;
2、文章内容系作者个人观点,不代表除个人作者外的任何第三方针对观点赞同或支持;
3、本站文章均为博主原创,如需转载,请注明文章来源;