0

Trying to implement a simple API endpoint on a Wordpress site. I cannot seem to get the end of the URL to pass in to the callback...

function fc_category($request) {
    return func_get_args();
}

add_action( 'rest_api_init', function () {
    register_rest_route( 
        'freshstore', 
        '/category/(?P<id>.*+)', 
        array(
            'methods' => 'GET',
            'callback' => 'fc_category',
        ) 
    );
});

The return is an empty array:

[{}]

Tested my regex, it should be solid but... this is the first time I've tried to implement a Wordpress endpoint like this, so perhaps I am way off-base with how this works.

  • I see that $request is not being used here, how are you retrieving the ID? I would also version your route in case it changes in future, e.g. 'freshstore/v1' – Tom J Nowell Aug 25 '18 at 21:08
0

So the value is hidden away in the array passed in to the callback. Accessed via the named capture group:

$request['id']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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