This is why:
'methods' => 'POST',
Your REST API route is not being found because there is no route at that address that accepts a GET
request. There is an endpoint with that name that accepts POST
but it can only be accessed via a POST
request.
If you want to be able to visit it in a browser via the URL you need to do one of the following:
'methods' => 'GET',
'methods' => ['GET, 'POST' ],
Other things to note:
- In your endpoints callback, use the request object you were given to access the parameters and arguments, do not use
$_POST
or$_GET
- if you access them directly you bypass all the sanitisation and validation security features of the REST API, and have to write extra code to handle GET and POST that WP has already done for you
endpoint
is incredibly generic, and there’s a good reason the WP endpoints use a version number, this should be changed!- Anonymous functions like that may look nice but they’re awful to debug, don’t show up nicely in stack traces, and are a nightmare if you ever have to un-hook/re-hook the callback.
- If you’re just using a class as a place to put your functions then you’re not doing object oriented programming, and there’s no practical difference to functions in a namespace aside from all the extra typing. Save yourself from this turning into the god class anti-pattern
- a strong indicator of this is if your class has no member variables, is only constructed once, or contains your entire codebase