Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Created October 2, 2014 20:48
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 macbookandrew/e7ffe782ce1c1b80e64b to your computer and use it in GitHub Desktop.
Save macbookandrew/e7ffe782ce1c1b80e64b to your computer and use it in GitHub Desktop.
point search engines from dev to live WordPress site via rel="canonical"
/*
* 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