Skip to content

Commit 6dd7cde

Browse files
committed
Merge pull request #2 from aaemnnosttv/multisite
add support for sub-domain sub-sites in multisite
2 parents a22cc07 + 890a711 commit 6dd7cde

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

wp-stage-switcher.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Stage Switcher
44
Plugin URI: http://roots.io/plugins/stage-switcher/
55
Description: A WordPress plugin that allows you to switch between different environments from the admin bar.
6-
Version: 1.0.1
6+
Version: 1.0.2
77
Author: Ben Word
88
Author URI: http://roots.io/
99
License: MIT License
@@ -42,17 +42,28 @@ function admin_bar_stage_switcher($admin_bar) {
4242
'href' => '#'
4343
));
4444

45-
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
45+
$host_bits = explode('.', $_SERVER['HTTP_HOST']);
46+
$is_ms_subdomain = (is_multisite() && defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL && count($host_bits) > 2);
4647

4748
foreach($stages as $stage => $url) {
4849
if ($stage === $current_stage) {
4950
continue;
5051
}
5152

52-
$url .= str_replace($stages[$current_stage], '', $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
53+
if ($is_ms_subdomain) {
54+
$subdomain = $host_bits[0];
55+
$stage_host = parse_url($url, PHP_URL_HOST);
56+
$subd_stage_host = "$subdomain.$stage_host";
57+
$url = str_replace($stage_host, $subd_stage_host, $url);
58+
$url = rtrim($url, '/') . $_SERVER['REQUEST_URI'];
59+
} else {
60+
$stage_scheme = parse_url($stages[$current_stage], PHP_URL_SCHEME);
61+
$cur_stage_base = str_replace("$stage_scheme://", '', $stages[$current_stage]);
62+
$url .= str_replace($cur_stage_base, '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
63+
}
5364

5465
$admin_bar->add_menu(array(
55-
'id' => $stage,
66+
'id' => "stage_$stage",
5667
'parent' => 'environment',
5768
'title' => ucwords($stage),
5869
'href' => $url

0 commit comments

Comments
 (0)