###Reverting to absolute url's###
Implementing root-relative url's was pretty much doable, but complicated because WP was written to use absolute url's and expects absolute url's. I realised that all I wanted was for people visiting my site to be able to see all links using the same domain as the one they specified. This didn't mandate using root-relative url's. So I've rewritten the code to replace the domains in all relevant url's with the domain specified by the visitor. The complete code is much simpler!
// set the url's domain to that of the current host, IGNORING any specified subdomains.
// (other WordPress users may need to retain the subdomains)
function gregory_use_host_domain( $url='' ) {
$myDomains = '(?:domain1|domain2|domain3|xn--domain4|xn--domain5)';
$matches = array();
// get the current host domain. ignore/remove any subdomain.
if( 1 != preg_match( '!.*('.$myDomains.'\.hk)!iu', $_SERVER['HTTP_HOST'], &$matches )) // preg_match returns 1 if the pattern was found.
// the current host is not one of the above specified domains! weird. this will probably never happen, but just in case...
return $url;
$thisDomain = ( is_ssl() ? 'https://' : 'http://' ).$matches[1];
if( 1 == preg_match( '!^(?:https?://[^/]*'.$myDomains.'\.hk)(/.*)?$!iu', $url, &$matches ))
// fully qualified url.
return $thisDomain.$matches[1];
if( substr( $url, 0, 1 ) == '/' )
// root-relative url.
return $thisDomain.$url;
// relative url but not root-relative.
return $thisDomain.'/'.$url;
}
add_filter( 'site_url', 'gregory_use_host_domain', 11, 1 );
add_filter( 'home_url', 'gregory_use_host_domain', 11, 1 );
/*
* CONTENT_URL()
* this function directly affects the template_ and stylesheet_ directory uri results,
* but they don't matter as far as the visitor is concerned. they're not going to directly
* access those particular url's.
*
* SPAM-FREE-WORDPRESS PLUGIN
* the Spam-Free-Wordpress plugin uses plugin_dir_url() and plugin_dir_path() to get the paths to its resources,
* but we're not going to worry about specifying the domain for these items.
* wp's plugin_dir_url() uses wp's plugins_url()
* wp's plugin_dir_path() uses php's dirname()
*/
// a filter to specify the 'domain1.hk' domain for WPSEO's canonical links.
// one specific domain for all content canonical links. no 'duplicate' content.
function gregory_wpseo_specify_canonical_domain( $canonical='' ) {
$matches = array();
if( 1 != preg_match( '!^(?:https?://[^/]+)(/.*)?$!iu', $canonical, &$matches ))
return $canonical;
// get_option is defined in wp-includes/functions.php and is used by get_home_url() to get the home url.
return get_option('home').$matches[1];
}
add_filter( 'wpseo_canonical', 'gregory_wpseo_specify_canonical_domain', 10, 1 );
cheers,
Gregory
(WordPress 3.4.2)