Skip to content

Instantly share code, notes, and snippets.

@RobMacKay
Last active November 14, 2019 18:19
Show Gist options
  • Save RobMacKay/526c0a8d73890df23d5f2d82791c8f8e to your computer and use it in GitHub Desktop.
Save RobMacKay/526c0a8d73890df23d5f2d82791c8f8e to your computer and use it in GitHub Desktop.
Simple CORS for WordPress with domain control.
//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