Search code examples
c#xamarinwordpress-rest-api

Wordpress REST API - send reset password link


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" }


Solution

  • 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