如何通过Wordpress Rest API重置用户密码

时间:2018-12-30 20:23:06

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

您认识的任何人都知道通过wordpress rest api发送丢失密码的重置链接吗?我一直在研究wordpress rest api文档,但尚未找到任何相关信息。也许有人为此做了一个自定义功能。

1 个答案:

答案 0 :(得分:0)

我找到了一种方法:

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'
) );

});