rest_api_init event not fired

Anton Putau picture Anton Putau · May 26, 2017 · Viewed 7.6k times · Source

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.

Answer

Gnanasekaran Loganathan picture Gnanasekaran Loganathan · Jun 19, 2017

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/