Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created October 16, 2012 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisguitarguy/3896992 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/3896992 to your computer and use it in GitHub Desktop.
A `sunrise.php` drop in that (mostly) allows custom domains in WordPress multisite. Still needs some more work, but it gets the job done.
<?php
/**
* A sunrise.php drop in that allows you to use WordPress' built in `domain`
* and `path` settings for MultiSite blogs.
*
* Some limitations:
* - Using this with a MS install and paths (not subdomains) does some
* weird stuff when updating `siteurl` and `home` while updating domain.
* So you'll you'll have to update those separately. There are probably
* filters to fix this.
* - Each blog's `siteurl` should still include the path to WordPress if you
* have WP installed in a subdirectory. The site will still work without
* this, but going to wp-admin on that site will not.
* - You can only have a single domain (and it's matching (non-)www partner)
* per site. This probably isn't a big deal: it's supid to build the
* same site on more than one domain.
* - Non-apache folks may have a more difficult time with permalinks. I couldn't
* get rid of `index.php` using nginx -- WordPress kept adding it back in.
*
* @author Christopher Davis <chris [AT] classicalguitar.org>
* @license GPLv2
*/
if(defined('COOKIE_DOMAIN') || defined('SITECOOKIEPATH'))
{
die('"COOKIE_DOMAIN" or "SITECOOKIEPATH" is defined.');
}
$dm_domain = $_SERVER['HTTP_HOST'];
if(($nowww = preg_replace('/^www\./', '', $dm_domain)) != $dm_domain)
{
$where = $wpdb->prepare('domain IN (%s, %s)', $dm_domain, $nowww);
}
else
{
$where = $wpdb->prepare('domain = %s', $dm_domain);
}
if($_blog = $wpdb->get_row("SELECT * FROM {$wpdb->blogs} WHERE {$where} LIMIT 1"))
{
$current_blog = $_blog;
$blog_id = $_blog->blog_id;
$site_id = $current_blog->site_id;
$current_site = $wpdb->get_row(
$wpdb->prepare("SELECT * from {$wpdb->site} WHERE id = %d", $site_id)
);
$current_site->blog_id = $blog_id;
$current_site = get_current_site_name($current_site);
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST']);
// WP uses $current_site->path to define this. We don't want that.
define('SITECOOKIEPATH', $current_blog->path);
}
@franz-josef-kaiser
Copy link

There's no reliable way to get the HTTP_HOST, but here's a maybe more safe way:

! empty( $_SERVER['SERVER_NAME'] ) AND $domain = $_SERVER['SERVER_NAME'];
! isset( $domain ) AND ! empty( $_SERVER['HTTP_HOST'] ) AND $domain = $_SERVER['HTTP_HOST'];

#fail and abort
if ( ! isset( $domain ) )
    return;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment