【问题标题】:Control the size of the minimum password required for profile modification控制配置文件修改所需的最小密码大小
【发布时间】:2021-10-08 16:08:03
【问题描述】:

我已经尝试了几个小时来让最小密码大小控制系统工作。它适用于注册,但对于个人资料修改我无法让它工作。我有一个函数 update_user 结束如下:

// The password is updated only if a new one has been filled in
    if (isset($_POST['user_pass']) && !empty($_POST['user_pass'])) {
      $userdata['user_pass'] = trim($_POST['user_pass']);
    }

// We check that the password contains 8 characters 
    if ( strlen($_POST['user_pass']) < 8 ) {
      $errors = 'Your password must be at least 8 characters long and have at least one capital letter and one number in it.';
    }
      
// Update user
    wp_update_user($userdata);

// Redirect
    wp_redirect(site_url('/profile'));
    exit();
  }
}

我也尝试过单独执行一个函数,但也没有用。

【问题讨论】:

  • 我不是 WordPress 开发人员,但我没有在您的代码中看到阻止用户更新的内容。您设置了一个 $errors 变量,但我认为它不会做太多事情。但这能满足您的需求吗? wordpress.stackexchange.com/a/159883
  • 确实我添加了一个else,如果满足条件,尝试保存密码修改。还是不行:if ( strlen($_POST['user_pass']) &lt; 8 ) { $error = 'Your password must be at least 8 characters long and have at least one capital letter and one number in it.'; } else { $user = wp_update_user(array( user_pass' =&gt; $d['user_pass'], )); if (is_wp_error($user)) { $error = $user-&gt;get_error_message(); } } }

标签: php wordpress passwords string-length


【解决方案1】:

确实,它错过了在密码检查后更新用户的条件。我试过你的链接,它不适用于我的主题。 在我的 profile.php 文件中,我有这个调用错误的代码:

 <?php if ($error) : ?>
    <div class="alert alert-danger align-items-center" role="alert">
      <i class="fas fa-exclamation"></i>&nbsp;<?php echo $error; ?><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
 <?php endif ?>

我的 else 没有成功如下:

if ( strlen($_POST['user_pass']) < 8 ) {
$error = 'Your password must be at least 8 characters long and have at least one capital letter and one number in it.';
} else {
    $user = wp_update_user(array(
    'user_pass'  => $d['user_pass'],
  ));
    if (is_wp_error($user)) {
    $error = $user->get_error_message();
 }
}

【讨论】:

    【解决方案2】:

    我对 WordPress 不是特别熟悉,但您的脚本流程对我来说看起来不正确,所以也许我可以帮助填补空白。

    您的问题在这里:

    if ( strlen($_POST['user_pass']) < 8 ) {
        $errors = 'Your password must be at least 8 characters long and have at least one capital letter and one number in it.';
    }
    

    如果密码太短,你唯一要做的就是设置一个变量。然后脚本照常继续。

    您需要以某种方式将错误返回到配置文件页面并呈现它,因为就目前而言,它将始终不断更新用户的数据而不会阻止执行。

    阅读材料

    Enforcing password complexity

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 2018-06-19
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多