Skip to content

Commit b50f711

Browse files
authored
Add README section explaining how to enable auto-discovery over CORS (#307)
As noted in #303, our documentation does not cover how to enable the REST API to be discovered with a cross-origin request. This PR adds a new section to the readme to explain the process. Fixes #303
1 parent e2c89fd commit b50f711

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ apiPromise.then(function( site ) {
150150
})
151151
```
152152

153+
#### Cross-Origin Auto-Discovery
154+
155+
When attempting auto-discovery against a remote server in a client-side environment, discovery will fail unless the server is configured for [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) (CORS). CORS can be enabled by specifying a set of `Access-Control-` headers in your PHP code to instruct browsers that requests from remote clients are accepted; these headers also let you control what specific methods and links are exposed to those remote clients.
156+
157+
The [WP-REST-Allow-All-Cors](https://github.com/ahmadawais/WP-REST-Allow-All-CORS) plugin will permit CORS requests for all API resources. Auto-discovery will still fail when using this plugin, however, because discovery depends on the presence of a `Link` header on WordPress pages outside of the root REST API endpoint.
158+
159+
To permit your site to be auto-discovered by client-side REST API clients, add a filter to `send_headers` to explicitly whitelist the `Link` header for `HEAD` requests:
160+
161+
```php
162+
add_action( 'send_headers', function() {
163+
if ( ! did_action('rest_api_init') && $_SERVER['REQUEST_METHOD'] == 'HEAD' ) {
164+
header( 'Access-Control-Allow-Origin: *' );
165+
header( 'Access-Control-Expose-Headers: Link' );
166+
header( 'Access-Control-Allow-Methods: HEAD' );
167+
}
168+
} );
169+
```
170+
171+
Enable CORS at your own discretion. Restricting `Access-Control-Allow-Origin` to a specific origin domain is often preferable to allowing all origins via `*`.
172+
153173
### Bootstrapping
154174

155175
If you are building an application designed to interface with a specific site, it is possible to sidestep the additional asynchronous HTTP calls that are needed to bootstrap the client through auto-discovery. You can download the root API response, *i.e.* the JSON response when you hit the root endpoint such as `your-site.com/wp-json`, and save that JSON file locally; then, in
@@ -416,8 +436,8 @@ It is also possible to add your own slug-oriented query parameters to a site tha
416436

417437
Just as `.categories()` and `.tags()` can be used to return posts that are associated with one or more taxonomies, two methods exist to exclude posts by their term associations.
418438

419-
- `.excludeCategories()` is a shortcut for `.param( 'categories_exclude', ... )` which excludes results associated with the provided category term IDs
420-
- `.excludeTags()` is a shortcut for `.param( 'tags_exclude', ... )` which excludes results associated with the provided tag term IDs
439+
- `.excludeCategories()` is a shortcut for `.param( 'categories_exclude', ... )` which excludes results associated with the provided category term IDs
440+
- `.excludeTags()` is a shortcut for `.param( 'tags_exclude', ... )` which excludes results associated with the provided tag term IDs
421441

422442
**Custom Taxonomies**
423443

0 commit comments

Comments
 (0)