Skip to content

Instantly share code, notes, and snippets.

@lunaroja
Created March 3, 2015 06:51
Show Gist options
  • Save lunaroja/c79bf62f528e457d9abe to your computer and use it in GitHub Desktop.
Save lunaroja/c79bf62f528e457d9abe to your computer and use it in GitHub Desktop.
Set CORS Header for WordPress JSON API
<?php
add_action( 'init', 'handle_preflight' );
function handle_preflight() {
header("Access-Control-Allow-Origin: " . get_http_origin());
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept");
if ( 'OPTIONS' == $_SERVER['REQUEST_METHOD'] ) {
status_header(200);
exit();
}
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment