Skip to content

Instantly share code, notes, and snippets.

@developdaly
Created December 3, 2012 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developdaly/4195012 to your computer and use it in GitHub Desktop.
Save developdaly/4195012 to your computer and use it in GitHub Desktop.
WordPress network sunrise.php for multiple environments with a single database
<?php
// Check the current host or server name
if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
} elseif (isset($_SERVER['SERVER_NAME'])) {
$host = $_SERVER['SERVER_NAME'];
}
// Switch the host to the desired domain
if ( DOMAIN_CURRENT_SITE == $host && WP_LOCAL_DEV ) {
$host = 'staging.example.com';
}
if (is_subdomain_install()) {
$sql = $wpdb -> prepare("SELECT * FROM {$wpdb->blogs} WHERE domain = %s LIMIT 1", $host);
} else {
// Get the path from the URl
$path = explode('/', ltrim($_SERVER['REQUEST_URI'], '/'));
if (count($path))
$path = '/' . $path[0];
else
$path = '/';
$sql = $wpdb -> prepare("SELECT * FROM {$wpdb->blogs} WHERE path in ('{$path}', '{$path}/') AND domain = %s LIMIT 1", $host, $path);
}
if ($_blog = $wpdb -> get_row($sql)) {
$current_blog = $_blog;
$blog_id = $_blog -> blog_id;
$site_id = $current_blog -> site_id;
// set current site
$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);
}
@developdaly
Copy link
Author

Credit for this goes to chrisguitarguy for his answer on Stack Exchange.

@royduin
Copy link

royduin commented Jan 9, 2015

get_current_site_name is deprecated since Wordpress 3.9

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