20

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.

Anton Putau
  • 632
  • 1
  • 7
  • 31
  • 1
    Do you have pretty permalinks enabled? – Ty Bailey May 31 '17 at 20:34
  • @TyBailey, in permalinks settings I have 'Day and name' structure. – Anton Putau Jun 01 '17 at 08:31
  • Can you try a static page and see if that works? Rule out something going on with .htaccess or some other routing rule somewhere else in your code. Don't forget there is a precendence as well. Might want to add the fourth parameter as true. – Shawn Jun 01 '17 at 20:52

5 Answers5

9

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.

Shakeel Ahmed
  • 1,526
  • 17
  • 22
7

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/

1

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
0

Using the latest build I'm not seeing the rest_api_init action being fired. Looks like this code in the plugin.php is always empty and returns, never allowing the rest_api_init action to be fired:

if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
    return;
}
Dharmesh Hadiyal
  • 719
  • 1
  • 7
  • 18
-1

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

Ian Elvister
  • 357
  • 3
  • 9