Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.