1

So I have created a custom endpoint using WP-API plugin. When testing with Postman, the endpoint works as expected.

The problem is that when I try to access the custom enpoint using the built in Backbone JS client, I cannot see my new endpoints in the wp.api.models object.

I have been digging and I think I have to send initialise it with a different schema or something, but have aboslutely no idea how to and can't find any info on how to extend wp.api.models so I can access my custom methods.

Example plugin code:

function my_awesome_func( $data ) {
    return 'A STRING';
}

function initrest() {
    register_rest_route( 'wp/v2', '/score', array(
        'methods' => 'GET',
        'callback' => 'my_awesome_func',
    ));
} 

add_action( 'rest_api_init', 'initrest');

Example template code:

window.onload = function() {
    console.log(wp.api.models);
};

This ouputs a list of the common models for Post, Comment, Tags etc, but no sign of my 'scores' endpoint.

karatecode
  • 574
  • 6
  • 21

2 Answers2

0

Try initializing the wp api with your own route.

wp.api.init({'versionString' : 'custom-route/v1/',  'apiRoot': 'http://my-website.com/wp-json/'}).done(function()
{ 
  console.log(wp.api.models);
}); 

Whereas versionString and apiRoot need to be replaced with your own data.

dave
  • 101
0

I'm afraid custom endpoints don't make into the wp.api.models by design of the Backbone client. Have a look to my question and the respective answers. They boil down to: "use a different client".

I use node-wpapi.

Lucio Crusca
  • 1,277
  • 3
  • 15
  • 41