Created
March 6, 2024 21:36
-
-
Save eitchugo/d7f8f885d67bed61a74583a1e280800f to your computer and use it in GitHub Desktop.
Sunrise to allow different domains for administration on a Wordpress multi-site blog
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 | |
/** | |
* File sunrise.php | |
* | |
* This file is read and run before most of wordpress functionality. Here we will allow | |
* all multi-site blogs administration interfaces to work on another domain (usually inside | |
* a VPN). | |
*/ | |
if (!function_exists('sunrise_modify_domain')): | |
/** | |
* Replaces the origin domain with the masked domain. This uses the constants | |
* `WP_MASKED_DOMAIN` and `DOMAIN_CURRRENT_SITE` to replace the string. | |
* | |
* @param string $url Full URL to replace the domain. | |
* | |
* @return string The replaced string. If constants are not defined, it will return the original url. | |
*/ | |
function sunrise_modify_domain($url) { | |
global $current_domain; | |
if (defined('WP_MASKED_DOMAIN') and defined('DOMAIN_CURRENT_SITE')) { | |
return str_replace(DOMAIN_CURRENT_SITE, $current_domain, $url); | |
} else { | |
return $url; | |
} | |
} | |
endif; | |
/** | |
* We limit this functionality only when using administration interface. Also, constant | |
* `WP_MASKED_DOMAIN` must be defined as an array of possible alternative domains. If you | |
* want *all domains*, use `$_SERVER['HTTP_HOST']` as a value in this array. | |
*/ | |
if ( | |
(defined('WP_MASKED_DOMAIN') and defined('DOMAIN_CURRENT_SITE')) and | |
(is_admin() or preg_match('/^\/([^\/]+\/)?(wp-login\.php|wp-json\/|wp-content\/)/', $_SERVER['REQUEST_URI'])) | |
) { | |
$current_domain = strtolower(stripslashes($_SERVER['HTTP_HOST'])); | |
if (str_ends_with($current_domain, ':80')) { | |
$current_domain = substr($domain, 0, -3); | |
$_SERVER['HTTP_HOST'] = substr($_SERVER['HTTP_HOST'], 0, -3); | |
} elseif (str_ends_with($current_domain, ':443')) { | |
$current_domain = substr($domain, 0, -4); | |
$_SERVER['HTTP_HOST'] = substr($_SERVER['HTTP_HOST'], 0, -4); | |
} | |
if (in_array($current_domain, WP_MASKED_DOMAIN)) { | |
$current_path = stripslashes($_SERVER['REQUEST_URI']); | |
$blog_path = preg_replace('/^(\/[^\/]+\/).*/', '\1', $current_path); | |
$blog_path = rtrim($blog_path, '/'); | |
$blog_path = $blog_path . '/'; | |
$blog_id = $wpdb->get_var( | |
$wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s", DOMAIN_CURRENT_SITE, $blog_path)); | |
if ($blog_id === null) { | |
$blog_id = $wpdb->get_var( | |
$wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s", DOMAIN_CURRENT_SITE, '/')); | |
} | |
$current_site = get_network(1); | |
$current_blog = get_site($blog_id); | |
add_filter('home_url', 'sunrise_modify_domain', 100, 1); | |
add_filter('site_url', 'sunrise_modify_domain', 100, 1); | |
//add_filter('upload_dir', 'sunrise_modify_domain', 10); | |
//add_filter('the_content', 'sunrise_modify_domain', 100, 1); | |
//add_filter('content_url', 'sunrise_modify_domain', 100, 1); | |
//add_filter(‘pre_option_home’, ‘dev_pre_url_filter’, 1 ); | |
//add_filter(‘pre_option_siteurl’, ‘dev_pre_url_filter’, 1 ); | |
//add_filter(‘post_thumbnail_html’, ‘dev_content_filter’, 100 ); | |
//add_filter(‘wp_get_attachment_link’, ‘dev_content_filter’, 100 ); | |
//add_filter(‘wp_get_attachment_url’, ‘dev_content_filter’, 100 ); | |
} | |
} | |
/* | |
// some hardass debugging... | |
echo "<p>current_domain: " . $current_domain . "</p>\n"; | |
echo "<p>current_path: " . $current_path . "</p>\n"; | |
echo "<p>blog_path: " . $blog_path . "</p>\n"; | |
echo "<p>blog_id: " . $blog_id . "</p>\n"; | |
echo "<p>current_site: "; var_dump($current_site); echo "</p>\n"; | |
echo "<p>current_blog: "; var_dump($current_blog); echo "</p>\n"; | |
exit(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References: