Skip to content

Instantly share code, notes, and snippets.

@patilvikasj
Created December 19, 2024 07:46
Show Gist options
  • Select an option

  • Save patilvikasj/c694248a92092f624b96e7380a584a8e to your computer and use it in GitHub Desktop.

Select an option

Save patilvikasj/c694248a92092f624b96e7380a584a8e to your computer and use it in GitHub Desktop.
Disable CORS on WordPress site for specific URL
<?php
/**
* Plugin Name: Disable CORS for Specific URL
* Description: Disables CORS headers for a specific URL in WordPress.
* Author: Your Name
* Version: 1.0
*/
add_action('send_headers', function () {
// Get the current URL path
$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// Specify the URL path for which CORS should be disabled
$path_to_disable_cors = 'whats-new/feed'; // Change this to your specific URL path
if ($current_path === $path_to_disable_cors) {
// Remove the CORS headers
header_remove('Access-Control-Allow-Origin');
header_remove('Access-Control-Allow-Methods');
header_remove('Access-Control-Allow-Headers');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Authorization');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment