rest_api_init event not fired rest_api_init event not fired wordpress wordpress

rest_api_init event not fired


Refer below check list,
1. Change your permalink as a pretty permalink and check.
2. Check your .htacess file (it should be writable when you save permalink structure it re-writable by wp).
3. Check Auth.
4. Check below custom endpoint creation method,

add_action( 'rest_api_init', function () {  register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(    'methods' => 'GET',    'callback' => 'my_awesome_func',  ) );} );

REF : https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/


I got solution, You need to use wp-json with your URL... like https://yourdomain.com/wp-json/namespace/and-so-on/

Then it will work. I was missing wp-json in URL.


In my case, the callback was actually a private method. I had to change it to a public method to get everything to work:

class Example {    function __construct() {        add_action( 'rest_api_init', [ $this, 'example_method' ] );    }    public function example_method() {        /* This will not work if the method is private! */        /* ... */    }}new Example();

On one installation, the method being private caused an error with a stack trace, but on another installation, the private method was simply not called and no errors were generated. I'm still not sure why one machine reacted one way and not the other, both had WP_DEBUG and WP_DEBUG_LOG set to true.