Skip to content

Instantly share code, notes, and snippets.

@leepowers
Created March 5, 2022 00:48
Show Gist options
  • Save leepowers/496ff69614d29687520bdf4027f9b998 to your computer and use it in GitHub Desktop.
Save leepowers/496ff69614d29687520bdf4027f9b998 to your computer and use it in GitHub Desktop.
WordPress CORS plugin
<?php
/**
* Plugin Name: CORSWP - CORS in WordPress
* Plugin URI: http://leepowers.co/
* Description: Enable CORSWP in WordPress
* Version: 0.0.1
* Author: Lee Powers
* Author URI: http://leepowers.co/
* Text Domain: cors-in-wp
*/
/**
* Output CORS for all preflight OPTIONS calls.
*/
function cors_in_wp_cors_preflight() {
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
status_header(200);
$this->cors_in_wp_headers();
die;
}
}
add_action("init", "cors_in_wp_cors_preflight");
/**
* Output CORS headers
*/
function cors_in_wp_headers() {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header("X-Frame-Options", "SAMEORIGIN");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment