Created
December 13, 2019 09:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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