Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmpel/a7567e49f083572340c16c62dbfbb505 to your computer and use it in GitHub Desktop.
Save rmpel/a7567e49f083572340c16c62dbfbb505 to your computer and use it in GitHub Desktop.
Combat CORS issues in WordPress Multisite
<?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