Automatically append link to end of copied post

Hello,

Is it possible to automatically append a line of text and link to the end of copied posts that is in the following format:

"This article originally appeared on (Site Name)", where (Site Name) links back to the original article with a rel=canonical tag?

Thanks for your help,

Ian

  • Ignacio

    Hi @Ian.

    There's not currently a way to do that but I'm adding some filters in the next version. You can add them manually (they will be there in the next version), just follow these steps:

    Open the file multisite-content-copier/inc/content-copier/content-copier-page.php (don't woryy about the name of the file, posts are copied from there) and you'll see this in line 107:

    if ( $new_post_id ) {
    			// Insert post meta
    			foreach ( $orig_post_meta as $post_meta ) {
    				$unserialized_meta_value = maybe_unserialize( $post_meta->meta_value );
    				update_post_meta( $new_post_id, $post_meta->meta_key, $unserialized_meta_value );
    			}
    		}

    You just need to add a line after "if ($new_post_id ) {" like this

    if ( $new_post_id ) {
    			do_action( 'mcc_copy_post', $this->orig_blog_id, $post_id, $new_post_id );
    			// Insert post meta
    			foreach ( $orig_post_meta as $post_meta ) {
    				$unserialized_meta_value = maybe_unserialize( $post_meta->meta_value );
    				update_post_meta( $new_post_id, $post_meta->meta_key, $unserialized_meta_value );
    			}
    		}

    So now you have a way to insert some code there. You'll need to create a new plugin (don't insert it in your theme file functions.php, it won't work that way) with the following code:

    add_action( 'mcc_copy_post', 'ian_append_line_when_copy_post', 10, 3 );
    function ian_append_line_when_copy_post( $source_blog_id, $source_post_id, $new_post_id ) {
    	$new_post = get_post( $new_post_id );
    	if ( ! empty( $new_post ) && get_post_type( $new_post ) == 'post' ) {
    		// This will work only for posts not pages or another custom post type
    
    		$source_blog_details = get_blog_details( $source_blog_id );
    		$source_blog_name = $source_blog_details->blogname;
    		$source_blog_url = $source_blog_details->siteurl;
    
    		$new_post = (array)$new_post;
    		$new_post['post_content'] .= '<p>This article originally appeared on <a href="' . $source_blog_url . '">' . $source_blog_name . '</a></p>';
    		wp_update_post( $new_post );
    	}
    }

    Regards.
    Ignacio.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.