Skip to content

Instantly share code, notes, and snippets.

@pravnkay
Created December 13, 2019 09:10
Show Gist options
  • Save pravnkay/711b4a8dfb6d892b6b547bef5b859976 to your computer and use it in GitHub Desktop.
Save pravnkay/711b4a8dfb6d892b6b547bef5b859976 to your computer and use it in GitHub Desktop.
Allow CORS for WP API - Along with sending Status 200 for OPTIONS request
add_action( 'init', 'handle_preflight' );
function handle_preflight() {
$origin = get_http_origin();
if ( $origin == 'http://localhost:8080' || $origin == 'https://yourapp.firebaseapp.com') {
header("Access-Control-Allow-Origin: " . $origin);
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Credentials: true");
header( 'Access-Control-Allow-Headers: Authorization' );
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