Skip to content

Instantly share code, notes, and snippets.

@tarex
Created April 4, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarex/f53fffb1b83248f3ffff87c8c59ca3f4 to your computer and use it in GitHub Desktop.
Save tarex/f53fffb1b83248f3ffff87c8c59ca3f4 to your computer and use it in GitHub Desktop.
WP Rest API Cors
function add_custom_headers() {
add_filter( 'rest_pre_serve_request', function( $value ) {
header( 'Access-Control-Allow-Headers: Authorization, X-WP-Nonce,Content-Type, X-Requested-With');
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
return $value;
} );
}
add_action( 'rest_api_init', 'add_custom_headers', 15 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment