Wordpress REST API - send reset password link
Asked Answered
R

4

5

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:

  1. Plugins where function was given in documentation but not worked for me
  2. Manualy called wp-login.php?action=lostpassword with body

{ "redirect_to": "", "user_login": "[email protected]", "wp-submit": "Neues+Passwort" }

Ripe answered 27/8, 2018 at 8:54 Comment(0)
M
6

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

Maturate answered 25/4, 2020 at 9:33 Comment(2)
the $wc_emails function is for woocommerce so is there any similar function in WordPress?Rhoades
This requires woocommerce. How do we do it without woocomerce?Gastroenterology
L
2

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 );
}
Lifesaver answered 27/8, 2018 at 9:9 Comment(1)
Oh, dont know i could call it like this. Thanks! I tried and worked for me! :-)Ripe
W
2

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.

Wrong answered 26/9, 2021 at 9:5 Comment(0)
H
0

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://mcmap.net/q/2030411/-how-to-make-a-password-reset-request-in-wordpress

Hopson answered 24/8, 2022 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.