Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpscholar/59f5708cba291a314375b2dedd104e1e to your computer and use it in GitHub Desktop.
Save wpscholar/59f5708cba291a314375b2dedd104e1e to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: WP REST API - Allow All CORS Requests
* Description: Adds headers to allow cross-origin requests to the WordPress REST API.
* Version: 1.0
* Plugin URI: https://gist.github.com/wpscholar/59f5708cba291a314375b2dedd104e1e
* Author: Micah Wood
* Author URI: https://wpscholar.com
*/
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;
});
}, 12 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment