Presentation on extending the WordPress REST API -- modifying default responses and creating custom endpoints
http://joshpress.net/extending-rest-api-talk
6. Why Extend The Defaults?
Are endpoints of a default endpoint close, but
missing a field?
Combine POST requests.
add_filter( 'the_content', function( $content ) { …
add_filter( 'template_includes', function( $template ) { ...
7. Why Make Your Own?
● Unique data sets -- custom queries, custom
tables, wrapping existing classes.
● Just the data you need.
● Replace admin-ajax
$query = new WP_Query( $args );
$collection = new my_class( $params );
global $wpdb;
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
8. Anatomy Of A REST Request
WP_REST_SERVER ??WP_REST_Request WP_REST_Response
19. register_rest_route() Defines
● Endpoints For Route
● Permissions for endpoints
● Transport methods for endpoints
● Fields for endpoints
● Callback for endpoints
● Schema
20. Endpoint URL
public function the_route() {
register_rest_route( 'swp_api', '/search',
array( )
);
}
Namespace
Route
21. Transport Method
public function the_route() {
register_rest_route( 'swp_api', '/search',
array(
'methods' => WP_REST_Server::READABLE,
)
);
}
GET