Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jayllellis/c4740b327926a81684f58de878c78f7f to your computer and use it in GitHub Desktop.
Save jayllellis/c4740b327926a81684f58de878c78f7f to your computer and use it in GitHub Desktop.
CORS for the WordPress REST API
<?php
function my_customize_rest_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 );
header( 'Access-Control-Allow-Headers: X-Requested-With' );
return $value;
} );
}
add_action( 'rest_api_init', 'my_customize_rest_cors', 15 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment