Extending the WP REST API
August 4, 2015 · View on GitHub
Sample code for extending the WordPress REST API. Slides are also available
Examples in this Code
- Add revision counts to posts:
register_api_field() - Add featured image URL and dimensions to posts:
rest_prepare_postfilter andadd_link() - Custom routes:
register_rest_route - Custom authentication:
determine_current_userfilter - Forcing API Link in headers to SSL:
rest_urlfilter - Disallowing non-SSL requests:
rest_pre_dispatchfilter - Non-JSON responses:
rest_pre_serve_requestfilter - Change base API URL prefix:
‘rest_url_prefix’filter - Hide media endpoing from non-authenticated users:
global $wp_post_types; $wp_post_types['attachment']->show_in_rest = is_user_logged_in(); - Disabled media endpoint:
rest_dispatch_requestfilter