I'm trying to set up a custom endpoint.
I've been following https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ but I can't get the route to register.
Below is my code.
<?php
add_action('rest_api_init', 'register_plugin_routes');
function register_plugin_routes()
{
register_rest_route( 'mchs/v1', '/test',
[
'methods' => 'GET',
'callback' => 'mchs_test'
]
);
register_rest_route( 'mchs/v1', '/search/?P<search_email>\S+',
[
'methods' => ['GET','POST'],
'callback' => 'mchs_search_by_email',
'args' => [
'search_email' => [
'validate_callback' => function( $param, $request, $key ) {
return is_email( $param );
}
]
]
]
);
}
function mchs_search_by_email( WP_REST_Request $request )
{
$param = $request->get_param('search_email');
return search_hubspot( $param );
}
function mchs_test ( WP_REST_REQUEST $request )
{
return "This is a test.";
}
The route I'm trying to access is http://site.test/wp-json/mchs/v1/search/[email protected]
Using http://site.test/wp-json/mchs/v1/[email protected] doesn't work either.
There is no indication of why anything is failing, all I get in response is
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}
I know the script is loading because I can call http://site.test/wp-json/mchs/v1/test and get a successful response.
What's wrong with my call to register_rest_route() ? Is it the way I'm trying to access it?