Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Created March 15, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajithrn/1cb2716f4cc7c6e5e0b53bd37e45b416 to your computer and use it in GitHub Desktop.
Save ajithrn/1cb2716f4cc7c6e5e0b53bd37e45b416 to your computer and use it in GitHub Desktop.
CORS for the WordPress REST API
/**
* CORS for the WordPress REST API
*/
function kms_wp_rest_api_cors() {
remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
add_filter('rest_pre_serve_request', function ($value) {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Expose-Headers: Link', false);
return $value;
});
}
add_action('rest_api_init', __NAMESPACE__.'\\kms_wp_rest_api_cors', 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment