如何通过wordpress rest api重置用户密码
Posted
技术标签:
【中文标题】如何通过wordpress rest api重置用户密码【英文标题】:How to reset user password by wordpress rest api 【发布时间】:2019-05-27 14:43:32 【问题描述】:你们中的任何人都知道通过 wordpress rest api 发送丢失密码的重置链接吗?我一直在研究 wordpress 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'
));
);
【讨论】:
以上是关于如何通过wordpress rest api重置用户密码的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Wordpress REST API 对 LearnDash 中受保护数据的远程访问进行身份验证?
React Native - 如何使用 JWT 令牌从 WordPress REST API 获取私人帖子
如何使用 REST API 在 keycloak 中重置用户密码