WordPress: How to create custom REST API route?

We can create route using rest_api_init action like :

Simply add below code to your theme functions.php file.

add_action( 'rest_api_init', function () {
    register_rest_route('wp/v2', 'forgot_password', array(
        'methods' => array('GET', 'POST'),
        'callback' => 'forgot_password'
    ));
} );

function forgot_password(){
    // YOUR CALLBACK FUNCTION STUFF HERE
}

forgot_password will define route URL address.

methods will define which method u want get request parameters like GET or POST.

callback will define callback function which is call when rest api will fire.

Leave a Comment

error code: 523