[Multisite Content Copier] cannonical metatag must be set to the origin

I've set up a new multisite (subdirectories) with no mapped domains (for now). If i use "Multisite Content Copier" plugin, every posts canonical meta tag references to itself. but it should reference to the original post it was copied from.
The problem is, that search engines like google recognize this as duplicate content, even if it's on another domain.

Broadcast Plugin handles this correctly (it's an option which is active by default): https://wordpress.org/plugins/threewp-broadcast/

  • viobru
    • Recruit

    Hi, Marco!

    Hope you are doing great :slight_smile:

    I was able to replicate this on my testing site. I checked it and seems that this was requested before by another member and our devs provided some custom code to achieve this. I tested it and the code is working fine, so you should have no issues using it, but please first create a COMPLETE and UPDATED BACKUP of the site before adding it.

    Please try using this as a mu-plugin:

    <?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 );

    Please note that this code will ONLY work for new posts that you copy from now on.

    Hope this helps!

    Kind regards,
    Violeta

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.