当前位置:首页文章笔记建站教程WordPress 注册直接输入密码并检测密码强度

WordPress 注册直接输入密码并检测密码强度

默认情况下,WordPress 用户在注册时仅要求填写用户名和电子邮件,密码将由发送的密码重置链接进行设置。文本分享一段最新 WordPress 注册直接输入密码代码,用户注册时自动生成随机密码,并检测密码强度,让用户自己选择使用随机密码或者直接输入密码。将代码添加到前主题函数模板 functions.php 中:

  1. // 添加输入密码表单
  2. add_action( ‘register_form’, function () { ?>
  3. <div class="user-pass1-wrap">
  4. <p>
  5. <label for="pass1"><?php _e( ‘Password’ ); ?></label>
  6. </p>
  7. <div class="wp-pwd">
  8. <input type="password" data-reveal="1" data-pw="<?php echo esc_attr( wp_generate_password( 16 ) ); ?>" name="pass1" id="pass1" class="input password-input" size="24" value="" autocomplete="off" aria-describedby="pass-strength-result" />
  9. <button type="button" class="button button-secondary wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e( ‘Hide password’ ); ?>">
  10. <span class="dashicons dashicons-hidden" aria-hidden="true"></span>
  11. </button>
  12. <div id="pass-strength-result" class="hide-if-no-js" aria-live="polite"><?php _e( ‘Strength indicator’ ); ?></div>
  13. </div>
  14. <div class="pw-weak">
  15. <input type="checkbox" name="pw_weak" id="pw-weak" class="pw-checkbox" />
  16. <label for="pw-weak"><?php _e( ‘Confirm use of weak password’ ); ?></label>
  17. </div>
  18. </div>
  19. <p class="user-pass2-wrap">
  20. <label for="pass2"><?php _e( ‘Confirm new password’ ); ?></label>
  21. <input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" />
  22. </p>
  23. <p class="description indicator-hint"><?php echo wp_get_password_hint(); ?></p>
  24. <?php
  25. });
  26. // 加载验证JS
  27. add_action( ‘login_enqueue_scripts’, function () {
  28. if ( is_on_registration_page() && !wp_script_is( ‘user-profile’ ) ) {
  29. wp_enqueue_script(‘user-profile’);
  30. }
  31. });
  32. // 验证
  33. function is_on_registration_page() {
  34. return $GLOBALS[‘pagenow’] == ‘wp-login.php’ && isset( $_REQUEST[‘action’] ) && $_REQUEST[‘action’] == ‘register’;
  35. }
  36. // 错误提示
  37. add_filter( ‘registration_errors’, function ( $errors ) {
  38. if ( empty( $_POST[‘pass1’] ) ) {
  39. $errors->add( ‘password-required’, ‘<strong>Error</strong>: Please enter a password.’ );
  40. }
  41. if ( empty( $_POST[‘pass2’] ) ) {
  42. $errors->add( ‘password-required’, ‘<strong>Error</strong>: Please enter a password confirmation.’ );
  43. }
  44. return $errors;
  45. });
  46. // 生成随机密码
  47. add_filter( ‘random_password’, function ( $password ) {
  48. if ( is_on_registration_page() && ! empty( $_POST[‘pass1’] ) ) {
  49. $password = $_POST[‘pass1’];
  50. }
  51. return $password;
  52. });
  53. // 自定义邮件内容
  54. add_filter( ‘wp_new_user_notification_email’, function ( $wp_new_user_notification_email, $user ) {
  55. $message = sprintf( __( ‘Username: %s’ ), $user->user_login ) . "\r\n\r\n";
  56. $message .= __( ‘Password: As entered during your registration’ ) . "\r\n\r\n";
  57. $message .= wp_login_url() . "\r\n";
  58. $wp_new_user_notification_email[‘message’] = $message;
  59. return $wp_new_user_notification_email;
  60. }, 10, 2 );

密码强度不够,需要勾选“确认使用弱密码”,否则注册按钮呈灰色不可点击。

如果想在 WP 默认注册页面和前端注册表单中同时应用这个检测功能,需要再加上:

  1. // 在前端加载验证JS
  2. add_action( ‘wp_footer’, function () {
  3. if ( !wp_script_is( ‘user-profile’ ) ) {
  4. wp_enqueue_script(‘user-profile’);
  5. }
  6. });

并在按钮 HTML 代码中添加:id=”wp-submit”

温馨提示:

文章标题:WordPress 注册直接输入密码并检测密码强度

文章链接:https://www.wuyanshuo.cn/1373.html

更新时间:2022年09月21日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

给TA打赏
共{{data.count}}人
人已打赏
建站教程

WordPress 自定义注册登录错误信息文字

2022-9-21 13:58:40

建站教程

WordPress 去掉菜单项链接A标签

2022-9-21 17:04:13

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'