Skip to content

Instantly share code, notes, and snippets.

@yornaath
Last active June 23, 2018 09:20
Show Gist options
  • Save yornaath/123f275e2755edc57b884c307f9b8a02 to your computer and use it in GitHub Desktop.
Save yornaath/123f275e2755edc57b884c307f9b8a02 to your computer and use it in GitHub Desktop.
<?php
/**
* Only accept cross origin from certain domains.
* Netlify browser app and localhost for development.
*/
add_action( 'rest_api_init', function() {
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_pre_serve_request', function( $value ) {
$current_origin = get_http_origin();
$allowed_origins = array(
"https://monumentplay.netlify.com", // the podcast app
"https://mnmt.no", // allow from root and not only www.
"http://localhost:9000" // for development
);
if($current_origin && in_array($current_origin, $allowed_origins)) {
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $current_origin ) );
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
}
return $value;
});
}, 15 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment