0

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?

1
  • Have you flushed your permalinks? Jul 8, 2022 at 1:14

1 Answer 1

0

What's wrong with my call to register_rest_route() ? Is it the way I'm trying to access it?

This is the correct way to access that endpoint:

The route I'm trying to access is http://site.test/wp-json/mchs/v1/search/[email protected]

But then you're getting the 404 error because the path variable in the second parameter below, is not in the correct form of (?P<parameter name>regular expression pattern), including the round brackets ( ( and ) ):

register_rest_route( 'mchs/v1', '/search/?P<search_email>\S+',

So you're just missing the round brackets and thus just add them, i.e. use (?P<search_email>\S+), and the 404 error would be gone:

register_rest_route( 'mchs/v1', '/search/(?P<search_email>\S+)',

Additionally, you should always set a permission_callback, even for public REST API routes, to avoid a _doing_it_wrong notice. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.