Created
July 17, 2015 21:29
-
-
Save Shelob9/87f9474df0f541e07383 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Use * for origin | |
*/ | |
add_action( 'rest_api_init', function() { | |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); | |
add_filter( 'rest_pre_serve_request', function( $value ) { | |
header( 'Access-Control-Allow-Origin: *' ); | |
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' ); | |
header( 'Access-Control-Allow-Credentials: true' ); | |
return $value; | |
}); | |
}, 15 ); | |
/** | |
* Only allow GET requests | |
*/ | |
add_action( 'rest_api_init', function() { | |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); | |
add_filter( 'rest_pre_serve_request', function( $value ) { | |
$origin = get_http_origin(); | |
if ( $origin ) { | |
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) ); | |
} | |
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( site_url() ) ); | |
header( 'Access-Control-Allow-Methods: GET' ); | |
return $value; | |
}); | |
}, 15 ); | |
/** | |
* Only allow same origin | |
*/ | |
add_action( 'rest_api_init', function() { | |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); | |
add_filter( 'rest_pre_serve_request', function( $value ) { | |
header( 'Access-Control-Allow-Origin: ' . esc_url_raw( site_url() ) ); | |
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' ); | |
header( 'Access-Control-Allow-Credentials: true' ); | |
return $value; | |
}); | |
}, 15 ); | |
/** | |
* Only from certain origins | |
*/ | |
add_action( 'rest_api_init', function() { | |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); | |
add_filter( 'rest_pre_serve_request', function( $value ) { | |
$origin = get_http_origin(); | |
if ( $origin && in_array( $origin, array( | |
//define some origins! | |
) ) ) { | |
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' ); | |
} | |
return $value; | |
}); | |
}, 15 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment