Last active
August 29, 2015 14:11
-
-
Save mattmckenny/0fb7f3ac2dfe3a26c31d to your computer and use it in GitHub Desktop.
WordPress : Always force original domain when previewing posts and pages when using domain mapping
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 | |
/** | |
* Forces original site for previewing posts and pages when using domain mapping | |
* | |
* @return URL of preview link using original site | |
**/ | |
function original_site_preview_link($url, $post) { | |
$parts = parse_url($url); | |
$host = $parts['host']; | |
$path = $parts['path']; | |
$path_segments = explode('/', $path); | |
$query = ('' != $parts['query']) ? "?{$parts['query']}" : ''; | |
$mapped = !strpos(network_site_url(), $host); | |
if ( $mapped ) { | |
// Remove leading slash only | |
$path_segments = array_slice($path_segments, 1); | |
} else { | |
// Remove array elements for leading slash and blogname | |
// If the domain isn't mapped, strip out the blog name from path, we'll add it later | |
$path_segments = array_slice($path_segments, 2); | |
} | |
// Put the path back together | |
$path = implode($path_segments, '/'); | |
// Blog site URL on original network domain | |
$site = trailingslashit(site_url()); | |
// Return new link | |
return $site . $path . $query; | |
} | |
add_filter( 'preview_post_link', 'original_site_preview_link'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment