Created
December 3, 2012 13:22
-
-
Save developdaly/4195012 to your computer and use it in GitHub Desktop.
WordPress network sunrise.php for multiple environments with a single database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
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
Credit for this goes to chrisguitarguy for his answer on Stack Exchange.