Problem creating a custom route in wordpress

670 Views Asked by At

Hello I am having an error whenever I try to create a custom route in WordPress. I am creating my own API and I want to create a custom url route like this:

add_action('rest_api_init', 'universityRegisterSearch');

function universityRegisterSearch() {
  register_rest_route('university/v1', 'search', array(
    'methods' => WP_REST_SERVER::READABLE,
    'callback' => 'universitySearchResults'
  ));
}

function universitySearchResults() {
  return 'Congratulations, you created a route.';
}

and I keep getting this error every time:

404 Error with Custom URL Route

enter image description here

This is the url i am testing with: http://fictional-university.local/wp-json/university/v1/search

Is there another solution to this? Thanks!

1

There are 1 best solutions below

4
Con On

It looks to me you are doing everything correctly. Using your function I can open the custom route, no problem.

Image Link: https://d.pr/i/5HhqvT

URL: /wp-json/university/v1/search - maybe try a conflict check next or maybe your custom function is not properly running.

Kind regards,