Created
March 17, 2025 07:25
-
-
Save rmpel/a7567e49f083572340c16c62dbfbb505 to your computer and use it in GitHub Desktop.
Combat CORS issues in WordPress Multisite
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 | |
// Make local URLs domainless, for all styles and scripts, to combat CORS issues. | |
// Ideally, we fix this in .htaccess using conditional headers, but in case of nginx, that's not available. | |
add_filter( 'script_loader_src', 'make_local_urls_domainless', 10 ); | |
add_filter( 'style_loader_src', 'make_local_urls_domainless', 10 ); | |
function make_local_urls_domainless( $url ) { | |
global $wpdb; | |
$all_domains = $wpdb->get_col( "SELECT domain FROM {$wpdb->blogs} ORDER BY blog_id DESC" ); | |
$all_domains_regex = implode( '|', $all_domains ); | |
$url = preg_replace( "@https?://(www.)?($all_domains_regex)/@i", '/', $url ); | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment