Created
December 19, 2024 07:46
-
-
Save patilvikasj/c694248a92092f624b96e7380a584a8e to your computer and use it in GitHub Desktop.
Disable CORS on WordPress site for specific URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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