【问题标题】:Issue when updating the password with wp_update_user()使用 wp_update_user() 更新密码时出现问题
【发布时间】:2017-11-14 23:38:48
【问题描述】:

我正在创建一个具有前端配置文件更新部分的插件。

如果我有这个代码...

$userdata = array(
    'user_login'    =>   $username,
    'user_email'    =>   $email,
    'user_url'      =>   $website,
    'first_name'    =>   $first_name,
    'last_name'     =>   $last_name,
    'nickname'      =>   $nickname,
    'description'   =>   $bio,
);

global $current_user; get_currentuserinfo();
$ID=$current_user->ID;
$ID_array = array('ID' => $ID);
$userdata = $ID_array + $userdata;

$user = wp_update_user( $userdata );

...一切都很好;但是,如果我还通过添加 'user_pass' => $password 来更新密码,从而将代码更改为...

$userdata = array(
    'user_login'    =>   $username,
    'user_email'    =>   $email,
    'user_pass'     =>   $password,
    'user_url'      =>   $website,
    'first_name'    =>   $first_name,
    'last_name'     =>   $last_name,
    'nickname'      =>   $nickname,
    'description'   =>   $bio,
);

global $current_user; get_currentuserinfo();
$ID=$current_user->ID;
$ID_array = array('ID' => $ID);
$userdata = $ID_array + $userdata;

$user = wp_update_user( $userdata );

...我在前端收到以下错误...

警告:无法修改标头信息 - 标头已由 /home/sdglandl/public_html/home 中的(输出开始于 /home/sdglandl/public_html/home/wp-content/themes/converio/header.php:2)发送/wp-includes/pluggable.php 在第 942 行

同样的错误在多行反复出现

【问题讨论】:

  • 尝试使用userpass 而不是user_pass
  • @mmm 没用

标签: wordpress


【解决方案1】:

当您更新当前用户的密码时,当前用户的 cookie 将被清除。这类似于已经讨论过的issue。您需要在呈现任何 HTML 之前运行此代码,以防止标头已设置问题。

add_action( 'wp', 'set_current_users_password' );

function set_current_users_password() {
    // Get current logged-in user.
    $user = wp_get_current_user();

    // Change password.
    wp_set_password($new_password, wp_get_current_user()->ID);

    // Log-in again.
    wp_set_auth_cookie($user->ID);
    wp_set_current_user($user->ID);
    do_action('wp_login', $user->user_login, $user);
}

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多