Hi I've built a wordpress plugin using a class. I'm sure all names (functies, urls, etc are correct. I'm trying to access my endpoint url using http://localhost/inschrijfmodule/wp-json/endpoint/sportivity

with this code

add_action('rest_api_init', function(){
            register_rest_route('endpoint', '/sportivity', [
                'methods' => 'POST',
                'callback' => [$this, 'sportivity_endpoint'],
                'permission_callback' => function () {
                    return true;

this is the error i'm receiving:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
  • did you resave/flush permalinks after adding the code? I assume pretty permalinks are enabled and working. Also your permission callback can be simplified to 'permission_callback' => '__return_true'
    – Tom J Nowell
    Nov 27, 2023 at 10:14
  • @TomJNowell I resaved the permalinks, they're set on postname. But I still have the same issue
    – Jan
    Nov 27, 2023 at 10:17
  • how did you confirm the code in question actually runs? If you place a wp_die( 'running' ); inside the function before register_rest_route does it die with running? You mentioned you used this inside a class but there is no class code in your question. It's also possible that because you used an anonymous function instead of a class method that $this doesn't refer to what you expect it to, the solution might be to create a public function register_rest_routes() : void { .... class method and call add_action('rest_api_init', [ $this, 'register_rest_routes' ]...
    – Tom J Nowell
    Nov 27, 2023 at 10:43
  • @TomJNowell the wp_die() came inside funtion before register_rest_route. I also tried your way like here pastebin.com/xiyjE8uD but this still does not solves the issue, besides that, it does not trigger the wp_die on the second spot
    – Jan
    Nov 27, 2023 at 10:55
  • so we can confirm register_rest_route is being called, can you add any more information about when where and how your class is being used to construct the object? Is it happening in the same file, on init, in functions.php etc?
    – Tom J Nowell
    Nov 27, 2023 at 14:19

1 Answer 1


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

Your Answer

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

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