rest_api_init event not fired

Viewed 12621

I am trying to run custom plugin on wp 4.7.4 . Below is my simple plugin

add_action( 'rest_api_init', 'register_routes');


function register_routes() {
   register_rest_route( 'taxonomy-manager/v1', '/taxonomies/(P<taxonomy_type>[a-zA-Z]+)', array(
   'methods' => 'GET',
   'callback' => 'get_or_insert'
  ) );
} 

function get_or_insert( WP_REST_Request $request ) {

   $parameters = $request->get_params();

   return $parameters;

}

When I request wp-json endpoint I see no plugin route there. Plugin was successfully activated. Have I missed something ? Does above plugin (or similar one based on rest_api_init event) works for anybody else ? Thanks.

5 Answers

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.

Check if your plugin is activated, it won't fire if it's not activated.

Related