Skip to content

Instantly share code, notes, and snippets.

@mattmckenny
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmckenny/0fb7f3ac2dfe3a26c31d to your computer and use it in GitHub Desktop.
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
<?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