Adding rel=canonical tags with Multisite Content Copier

To avoid duplicate content SEO penalties, it would be nice if the plugin could let you specify which site should "own" the copied content via a canonical tag.

ThreeWP Broadcast does this. We want to use Content Copier, but can't without this feature.

Please advise if Content Copier handles this otherwise.

Thanks,

inQbation

  • Ignacio

    Hi @inQbation

    There's not built in functionality for that in MCC but fortunately the plugin is pretty extensible so there's an easy way to do it. I'll describe to you how to add it but I have added this as a feature in future versions.

    So, here's the code you need.

    First of all, create a new file called, for instance, mcc-hooks.php and put it here:

    wp-content/plugins/mcc-hooks.php

    Then paste this code in it:

    <?php
    
    /**
     * Plugin Name: MCC Hooks
     */
    
    /**
     * Adds a canonical metadata to a post that has been
     * copied through Multisite Content Copier
     *
     * @param Integer $source_blog_id Blog ID where the source post is
     * @param Integer $source_post_id Source Post ID
     * @param Integer $new_post_id New Post ID
     */
    function mcc_hooks_add_canonical_meta( $source_blog_id, $source_post_id, $new_post_id ) {
    
    	$data = array(
    		'source_blog_id' => $source_blog_id,
    		'source_post_id' => $source_post_id
    	);
    
    	add_post_meta( $new_post_id, 'mcc_canonical_data', $data );
    
    }
    add_action( 'mcc_copy_post', 'mcc_hooks_add_canonical_meta', 10, 3 );
    
    /**
     * Add the canonical link in the header and removes
     * the WP built-in canonical link if canonical data
     * has been added to the post
     */
    function mcc_hooks_add_canonical_header() {
    	if ( is_single() ) {
    		$canonical_data = get_post_meta( get_the_ID(), 'mcc_canonical_data', true );
    
    		// Do we have canonical data for the current post?
    		if ( ! $canonical_data )
    			return;
    
    		$blog_details = get_blog_details( $canonical_data['source_blog_id'] );
    
    		// Does the source blog given by canonical data exist?
    		if ( empty( $blog_details->blog_id ) )
    			return;
    
    		// Let's get the source post URL
    		switch_to_blog( $canonical_data['source_blog_id'] );
    		$source_post_url = get_permalink( $canonical_data['source_post_id'] );
    		restore_current_blog();
    
    		// Does thesource post exist?
    		if ( ! $source_post_url )
    			return;
    
    		// Canonical data is valid, let's remove the WP built-in canonical link
    		remove_action( 'wp_head', 'rel_canonical' );	
    
    		// And add ours
    		?>
    			<link rel="canonical" href="<?php echo esc_url( $source_post_url ); ?>" />
    		<?php
    
    	}
    }
    add_action( 'wp_head', 'mcc_hooks_add_canonical_header', 5 );

    You'll see a new plugin in your network called MCC Hooks. Just network activate it. Every time you create a new post, canonical data will be assigned to that post and a canonical link should appear now in the header.

    Regards.
    Ignacio.

  • inqNS

    Hi Ignacio,

    Thanks for the prompt response!

    I implemented the code but unfortunately, it seems like it's missing some logic somewhere. In my test, the rel="canonical" tag is being applied to both the main site as well as the subsite(s) that I have copied the content to.

    My expectation is that the canonical tag should only be applied to the site that contains the copy—and not to the site that is the "content owner." Do I have that wrong?

    Thanks,

    Jeff

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.