Created
October 2, 2014 20:48
-
-
Save macbookandrew/e7ffe782ce1c1b80e64b to your computer and use it in GitHub Desktop.
point search engines from dev to live WordPress site via rel="canonical"
This file contains hidden or 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
/* | |
* Dev site: point search engines at live site | |
*/ | |
// set subdomain | |
$subdomain = 'dev'; | |
// test whether or not this is a dev site | |
if ( strpos( get_site_url(), $subdomain . '.' ) !== false ) { | |
remove_action( 'wp_head', 'rel_canonical' ); | |
add_action( 'wp_head', 'my_rel_canonical' ); | |
} | |
// rebuild canonical link | |
// slightly modified from the original rel_canonical function in /wp-includes/link-template.php | |
function my_rel_canonical() { | |
global $subdomain; | |
// original code | |
if ( ! is_singular() ) { | |
return; | |
} | |
global $wp_the_query; | |
if ( ! $id = $wp_the_query->get_queried_object_id() ) { | |
return; | |
} | |
// new code get current URL and strip dev subdomain | |
$URL = str_replace( $subdomain . '.', '', get_permalink( $id ) ); | |
if( $URL ) { | |
echo '<link rel="canonical" href="' . $URL . '" />'; | |
return; | |
} | |
// original code | |
$link = get_permalink( $id ); | |
if ( $page = get_query_var( 'cpage' ) ) { | |
$link = get_comments_pagenum_link( $page ); | |
echo '<link rel="canonical" href="' . $link . '" />'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment