Last active
November 14, 2019 18:19
-
-
Save RobMacKay/526c0a8d73890df23d5f2d82791c8f8e to your computer and use it in GitHub Desktop.
Simple CORS for WordPress with domain control.
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 it in your Functions file at the least | |
//Created a function to hook into the init. | |
// | |
function add_cors_http_header(){ | |
//An Array of allowed domains. | |
$allowed = array( | |
"http://localhost:3000", | |
"http://another-domain.com" | |
); | |
$origin = get_http_origin(); | |
if ( $origin && in_array($origin, $allowed) ) { | |
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) ); | |
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' ); | |
header( 'Access-Control-Allow-Credentials: true' ); | |
} | |
} | |
add_action('init','add_cors_http_header'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment