I host quite a few blogs on my network (WordPress Multisite) and have their static content served via a common CDN, by simply setting the wp-content directory URL in wp-config.php like so:
# http://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content_folder
define( 'WP_CONTENT_URL', 'http://abcdefghijk.cloudfront.net/wp-content' );
Now all static content (images, css, js, etc.) in wp-content directory, for all sites, is served via the common CDN.
The problem is, I need to use a different CDN/URL for wp-content directory for some sub-sites in the network.
So, the question is:
-
Is it possible to set different WP_CONTENT_URL
values for different sub-sites in wp-config.php?
-
If (1) isn’t possible, is it possible to define WP_CONTENT_URL
in functions.php instead? If yes, then is it possible to set different WP_CONTENT_URL
values for different sub-sites in functions.php?
Is it possible to set different WP_CONTENT_URL values for different sub-sites in wp-config.php ?
Ans: Yes it is. If sub sites are installed on sub-domain, An example to define it this way –
define( 'CURRENT_SITE_DOMAIN', $_SERVER['HTTP_HOST'] );
if( 'sub1.domain.com' == CURRENT_SITE_DOMAIN ){
define( 'WP_CONTENT_URL', 'http://sub1-abcdefghijk.cloudfront.net/wp-content' );
}
elseif( 'sub2.domain.com' == CURRENT_SITE_DOMAIN ){
define( 'WP_CONTENT_URL', 'http://sub2-abcdefghijk.cloudfront.net/wp-content' );
}
else{
define( 'WP_CONTENT_URL', 'http://abcdefghijk.cloudfront.net/wp-content' );
}
$_SERVER['HTTP_HOST']
will give you the domain host name, which comes without the protochol name (http[s]://)
and without any trailing slash (/)
Is it possible to define WP_CONTENT_URL in functions.php instead? If yes, then is it possible to set different WP_CONTENT_URL values for different sub-sites in functions.php ?
Ans: No it’s not. WP_CONTENT_URL
Constant is defined within a function wp_plugin_directory_constants()
which is called before the first hook for plugins muplugins_loaded
. So, the only option to define it on wp-config.php
file.