Skip to content

Instantly share code, notes, and snippets.

@cyrillbolliger
Last active January 17, 2023 16:10
Show Gist options
  • Save cyrillbolliger/0fc8f8d72273e5227890dfcf9d6c017a to your computer and use it in GitHub Desktop.
Save cyrillbolliger/0fc8f8d72273e5227890dfcf9d6c017a to your computer and use it in GitHub Desktop.
WordPress Multisite: Map multiple domains to single blog / subsite

WordPress Multisite: Map multiple domains to single blog / subsite

As the WordPress core can only assign a single domain to a multisite blog, one must employ the very early called sunrise.php, to setup multiple domains for a single blog in a WordPress Multisite environment. One use case for such a setup is a multilingual blog with one domain per language (e.g. when using Polylang with language selection by domain).

How To

  1. Create wp-content/sunrise.php if it does not exist
  2. Paste the snipped below into the sunrise.php
  3. Configure your extra domains:
    • call cybo_add_extra_domain( $domain, $blog_id ) with the additional domain and the id of the blog it should point to
    • you can call it multiple times for multiple domains
  4. Activate sunrise.php: Add define( 'SUNRISE', true ); to your wp-config.php

Credits

The sunrise.php is inspired by wpvip.com.

<?php
// Configure your additional domains here. Some examples:
//cybo_add_extra_domain( 'additional-domain-1.com', 123 );
//cybo_add_extra_domain( 'additional-domain-2.com', 123 );
//cybo_add_extra_domain( 'other-blog-extra-domain.com', 555 );
/**
* Map additional domain to blog / subsite in multisite setup
*
* @param string $domain The additional domain
* @param int $id The blog_id
*/
function cybo_add_extra_domain( $domain, $id ) {
if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
return;
}
$mask_domain = strtolower( $_SERVER['HTTP_HOST'] );
if ( $mask_domain !== $domain ) {
return;
}
global $blog_id, $current_blog, $current_site;
// Set globals
$blog_id = $id; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_blog = get_site( $blog_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// This should always be 1, unless you are running multiple WordPress networks.
$current_site = get_network( 1 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$origin_domain = $current_blog->domain . untrailingslashit( $current_blog->path );
$domain_replace_fn = function( $url ) use ( $mask_domain, $origin_domain ) {
return str_ireplace( $origin_domain, $mask_domain, $url );
};
add_filter( 'home_url', $domain_replace_fn, 1 );
add_filter( 'site_url', $domain_replace_fn, 1 );
add_filter( 'content_url', $domain_replace_fn, 1 );
add_filter( 'plugins_url', $domain_replace_fn, 1 );
add_filter( 'upload_dir', function( $uploads ) use ( $domain_replace_fn ) {
if ( ! empty( $uploads['url'] ) && is_string( $uploads['url'] ) ) {
$uploads['url'] = $domain_replace_fn( $uploads['url'] );
}
if ( ! empty( $uploads['baseurl'] ) && is_string( $uploads['baseurl'] ) ) {
$uploads['baseurl'] = $domain_replace_fn( $uploads['baseurl'] );
}
return $uploads;
}, 1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment