Reputation: 81
I want to create a request from my application. (iOS and Android - Coded with Xamarin).
Explaination:
The request should trigger the WordPress action=lostpassword
. - The user receives an email to reset his password in browser.
OR
The user will be able to set a new password. WordPress send a link to email - where the user has to verify, that he changed the password. Is there any chance to do this with a request to the REST API.
Maybe any similar ideas?
I have already tried:
wp-login.php?action=lostpassword
with body { "redirect_to": "", "user_login": "[email protected]", "wp-submit": "Neues+Passwort" }
Upvotes: 5
Views: 12085
Reputation: 533
Check this answer out for a working API call with Postman
to trigger sending password reset link in WordPress via RESTful API and working code for Flutter
https://stackoverflow.com/a/73467945/2218290
Upvotes: 0
Reputation: 124
You can create custom endpoint using this code inside function.php or any other place like custom plugin
function custom_user_forget_password($request)
{
$email = $request->get_param('email');
$userdata = get_user_by('email', $email);
if (empty($userdata)) {
$userdata = get_user_by('login', $email);
}
if (empty($userdata)) {
return __('User not found');
}
$user = new WP_User(intval($userdata->ID));
$reset_key = get_password_reset_key($user);
$wc_emails = WC()->mailer()->get_emails();
$wc_emails['WC_Email_Customer_Reset_Password']->trigger($user->user_login, $reset_key);
return __('Password reset link has been sent to your registered email.');
}
add_action('rest_api_init', function () {
register_rest_route('custom/v1/', '/forget_password', array(
'methods' => 'POST',
'callback' => 'custom_user_forget_password',
));
});
then you can call post request for this endpoint (URL /wp-json/custom/v1/forget_password) anywhere you want, and it will send a password reset link if the user exists.
Upvotes: 2
Reputation: 1240
Create your custom api
URL
https://yourdomain/api/forgot_password.php
Parameter
login:[email protected]
Create folder api in root and create file forgot_password.php
forgot_password.php
<?php include '../wp-load.php';
$login = $_REQUEST['login'];
if ( empty( $login ) ) {
$json = array( 'code' => '0', 'msg' => 'Please enter login user detail' );
echo json_encode( $json );
exit;
}
$userdata = get_user_by( 'email', $login);
if ( empty( $userdata ) ) {
$userdata = get_user_by( 'login', $login );
}
if ( empty( $userdata ) ) {
$json = array( 'code' => '101', 'msg' => 'User not found' );
echo json_encode( $json );
exit;
}
$user = new WP_User( intval( $userdata->ID ) );
$reset_key = get_password_reset_key( $user );
$wc_emails = WC()->mailer()->get_emails();
$wc_emails['WC_Email_Customer_Reset_Password']->trigger( $user->user_login, $reset_key );
$json = array( 'code' => '200', 'msg' => 'Password reset link has been sent to your registered email' );
echo json_encode( $json );
exit;
?>
login is parameter name keep in mind.
its working 100% for me try it
Upvotes: 6
Reputation: 500
I think this should help you.
Example shows return a lost password URL http://example.com/lostpassword/ for the wp_lostpassword_url() function:
add_filter( 'lostpassword_url', 'my_lost_password_page', 10, 2 );
function my_lost_password_page( $lostpassword_url, $redirect ) {
return home_url( '/lostpassword/?redirect_to=' . $redirect );
}
Upvotes: 2