I’m making ready a plugin for REST API integration (the WP 4.7 core model) by offering customized endpoints for knowledge from the plugin. One of many issues I’m attempting to do is to set a _links
property within the response and make the linked useful resource embeddable. Closely simplified code excerpt:
class Clgs_REST_Logs extends WP_REST_Controller {
const NSPACE = 'clgs';
const BASE = 'logs';
public perform register_routes() {
register_rest_route( self::NSPACE, '/' . self::BASE, array(
array(
'strategies' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( self::NSPACE, '/' . self::BASE . '/rely', array(
array(
'strategies' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_count' ),
),
'schema' => array( $this, 'get_count_schema' ),
) );
}
public perform get_items( $request ) {
$log_entries = // get an array
$assortment = $this->prepare_collection_for_response( $log_entries, $request );
$response = rest_ensure_response( array(
'objects' => $assortment
) );
$max_pages = max( 1, ceil( $total_items / (int) $params['per_page'] ) );
$this->set_count_headers( $response, (int) $total_items, (int) $max_pages );
$count_link = $this->prepare_link_url( array( self::BASE, 'rely' ) );
$response->add_links( array (
'rely' => array(
'href' => $count_link,
'embeddable' => true
),
) );
return $response;
}
public perform get_count( $request ) {
// present some statistics knowledge
}
public perform prepare_link_url ( $components = array() ) {
array_unshift( $components, self::NSPACE );
return rest_url( implode( '/', $components ) . '/' );
}
}
A response to a request http://wptesting.intern/index.php/wp-json/clgs/logs/?_embed
seems to be like this:
{
"objects": [...],
"_links": {
"rely": [{
"embeddable": true,
"href": "http://wptesting.intern/index.php/wp-json/clgs/logs/"
}]
},
"_embedded": {
"rely": [{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method",
"data": {"status":404}
}]
}
}
Requests to http://wptesting.intern/index.php/wp-json/clgs/logs/rely/
, if performed individually, succeeds.
I’ve performed this moreover with fairly premalinks disabled and http://wptesting.intern/index.php/?route=...
to make certain this isn’t a routing rewrite subject, and the identical factor occurs.
Has anybody an concept why the embedding fails?