I am moving my site from olddomain to newdomain. I want to keep all of the content at olddomain but I want the canonical version in google to be recognized as newdomain/whatever-post/ instead of the same thing at olddomain.
How can I modify the rel=canonical in the section of olddomain to make this change?
I am moving my site from olddomain to newdomain. I want to keep all of the content at olddomain but I want the canonical version in google to be recognized as newdomain/whatever-post/ instead of the same thing at olddomain.
How can I modify the rel=canonical in the section of olddomain to make this change?
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', 'newdomain', $link );
echo "<link rel='canonical' href='$link' />\n";
}
Obviously, just replace olddomain and newdomain in the second to last line with your actual domain names!
There is a filter !
add_filter( 'get_canonical_url', 'myfunc', 10, 2);
function myfunc( $canonical_url, $post )
{
...
...
return $canonical_url;
}