Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am moving my site from olddomain.com to newdomain.com. I want to keep all of the content at olddomain.com but I want the canonical version in google to be recognized as newdomain.com/whatever-post/ instead of the same thing at olddomain.com.

How can I modify the rel=canonical in the section of olddomain.com to make this change?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Unfortunately, there's no filter in the rel_canonical() function. But you can remove that function from wp_head altogether and write your own. Try adding this to the functions.php at your old domain:

remove_action( 'wp_head', 'rel_canonical' );
add_action( 'wp_head', 'new_rel_canonical' );

function new_rel_canonical() {
     if ( !is_singular() )
          return;

      global $wp_the_query;
      if ( !$id = $wp_the_query->get_queried_object_id() )
          return;

      $link = get_permalink( $id );
      $link = str_replace( 'olddomain.com', 'newdomain.com', $link );
      echo "<link rel='canonical' href='$link' />\n";
  }

Obviously, just replace olddomain.com and newdomain.com in the second to last line with your actual domain names!

share|improve this answer
    
Something relevant: gist.github.com/1217431 –  its_me Jul 17 '12 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.