This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_environment_switcher.php
87 lines (69 loc) · 2.17 KB
/
wp_environment_switcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
Plugin Name: WP Environment Switcher
Plugin URI: https://github.com/theideabureau/WP-Environment-Switcher
Version: 0.1
Author: Ben Everard (The Idea Bureau)
Description: Allows for easy switching of environments within the WordPress admin bar
Text Domain: wp-environment-switcher
License: GPLv3
*/
add_action('wp_head', 'wp_environment_switcher_styles');
add_action('admin_head', 'wp_environment_switcher_styles');
function wp_environment_switcher_styles() {
echo '<style>
#wpadminbar .environment-type {
float: left;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
height: 11px;
width: 11px;
background-color: red;
margin: 8px 8px 0 0;
}
#wp-admin-bar-switch-to-live .environment-type {
margin: 7px 7px 0 0;
}
#wpadminbar .environment-type-www {
background-color: green;
}
#wpadminbar .environment-type-staging {
background-color: orange;
}
#wpadminbar .environment-type-dev {
background-color: red;
}
</style>';
}
add_action('admin_bar_menu', 'wp_environment_switcher_menu', 1000);
function wp_environment_switcher_menu() {
global $wp_admin_bar;
$environment_labels = array(
'www' => 'Live',
'staging' => 'Staging',
'dev' => 'Development'
);
$sub_domain = current(explode('.', $_SERVER['HTTP_HOST']));
// if the sub_domain is not in the pre-existing list, set it to www
if ( ! isset($environment_labels[$sub_domain]) ) {
$sub_domain = 'www';
}
$current_environment = $environment_labels[$sub_domain];
$wp_admin_bar->add_menu(array(
'id' => 'environment_switch_menu',
'title' => '<span class="environment-type environment-type-' . $sub_domain . '"></span> Environment: ' . $current_environment,
'href' => false
));
foreach ( $environment_labels as $key => $environment ) {
// don't give the option to switchv
if ( $sub_domain === $key ) {
continue;
}
$wp_admin_bar->add_menu(array(
'parent' => 'environment_switch_menu',
'title' => '<span class="environment-type environment-type-' . $key . '"></span> Switch to ' . strtolower($environment),
'href' => 'http://' . str_replace($sub_domain . '.', $key . '.', $_SERVER['HTTP_HOST']) . $_SERVER['REQUEST_URI']
));
}
}