N
N
Ninja Mate2016-10-14 19:22:05
RESTful API
Ninja Mate, 2016-10-14 19:22:05

What is wrong with using my WP Rest API endpoint?

I'm trying to add a function to my theme through the rest api, where I specify the user id and reset his password.
Judging by the error, I'm somehow not registering correctly

jquery.min.js:4 POST http://*****.ap-southeast-2.compute.amazonaws.com/... 404 (Not Found)

functions.php
add_action( 'rest_api_init', 'register_api_hooks' );
function register_api_hooks() {

    register_rest_route('user',
        '/reset_pas/(?P<id>\d+)',
        array(
            'method' => 'POST',
            'post_callback' => 'reset_password_callback',
            'schema'          => null
        ));
}

function reset_password_callback($request ){
    
    $id=$request['id'];

    global $current_user;
    get_currentuserinfo();

    $user_to_reset = get_user_by( 'id', $id );
    $new_pass = wp_generate_password( 20, false );
    
    do_action( 'password_reset', $user_to_reset, $new_pass );

    wp_set_password( $new_pass, $user_to_reset->ID );
    update_user_option( $user_to_reset->ID, 'default_password_nag', false, true );

    do_action( 'after_password_reset', $user_to_reset, $new_pass );
}

JS
resetUserPassword(id){
        var that = this;
        $( document ).ready(function() {
            jQuery( document ).ready(function( $ ) {
                $.ajax({
                    'url': Slug_API_Settings.root + 'wp/v2/user/reset_pas/' + id,
                    'method': 'POST',
                    'beforeSend': function (xhr) {
                        xhr.setRequestHeader('X-WP-Nonce', Slug_API_Settings.nonce);
                    }/*,
                    data: {
                        'id': that.state.id
                    }*/
                }).done( (r)=>{
                    console.log(r);
                    //that.props.resetUsers
                } );
            });
        });
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loki9928, 2020-09-23
@Loki9928

because the post/ method needs to parse the json/ parameter first See the WP_REST_Request methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question