Last active
January 31, 2023 07:42
-
-
Save miya0001/d6508b9ba52df5aedc78fca186ff6088 to your computer and use it in GitHub Desktop.
CORS for the WordPress REST API
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 | |
function my_customize_rest_cors() { | |
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: GET' ); | |
header( 'Access-Control-Allow-Credentials: true' ); | |
header( 'Access-Control-Expose-Headers: Link', false ); | |
return $value; | |
} ); | |
} | |
add_action( 'rest_api_init', 'my_customize_rest_cors', 15 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I have a problem with several Access-Control-Allow headers additionally to already existing one (highlighted in orange in attached Screenshot). Could this my_customize_rest_cors function help to solve my problem?