【问题标题】:How to reset user password by wordpress rest api如何通过wordpress rest api重置用户密码
【发布时间】:2019-05-27 14:43:32
【问题描述】:

你们中的任何人都知道通过 wordpress rest api 发送丢失密码的重置链接吗?我一直在研究 wordpress rest api 文档,但我还没有找到任何关于它的信息。也许有人为此做了一个自定义函数。

【问题讨论】:

    标签: wordpress-rest-api woocommerce-rest-api


    【解决方案1】:

    我找到了一种方法:

    function runRetrivePassword($data)
    {
        global $wpdb, $wp_hasher;
        $user_data = get_user_by('email',  $data['email']);
        if (!$user_data) return array('result' => false);
    
        do_action('lostpassword_post');
        $user_login = $user_data->user_login;
        $user_email = $user_data->user_email;
        $key = get_password_reset_key($user_data);
        $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
        $message .= network_home_url('/') . "\r\n\r\n";
        $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
        $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
        $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
        $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');
    
        if (is_multisite())
            $blogname = $GLOBALS['current_site']->site_name;
        else
            $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    
        $title = sprintf(__('[%s] Password Reset'), $blogname);
        $title = apply_filters('retrieve_password_title', $title);
        $message = apply_filters('retrieve_password_message', $message, $key);
        if ($message && !wp_mail($user_email, $title, $message))
            wp_die(__('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...'));
    
        return array('result' => true);
    }
    
    add_action('rest_api_init', function () {
        register_rest_route('apiuser/v1', '/forgotpwd/(?P<email>\S+)', array(
            'methods' => 'GET',
            'callback' => 'runRetrivePassword'
        ));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2016-08-04
      • 2019-02-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多