1
votes

I have the following function to copy across a post as it's created on one site, to then move it over to a specific multisite blog:

function copy_across_to_multisite( $post_id ) {

// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
    return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
    return $post_id;

if ($post_id && get_current_blog_id() == 1) {
    $post = get_post($post_id, ARRAY_A); // get the original post
    if ($post->post_status == 'trash' || $post->post_status == 'auto-draft') {
        return $post_id;
    }
    $meta = get_post_meta($post_id);
    $post_thumbnail_id = get_post_thumbnail_id($post_id);
    $post_thumbnail = get_post($post_thumbnail_id, ARRAY_A);

    $post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post
    $post_thumbnail['ID'] = '';
    switch_to_blog(2); // switch to target blog
    $inserted_post_id = wp_insert_post($post); // insert the post
    $inserted_thumbnail = wp_insert_post($post_thumbnail);
    foreach($meta as $key=>$value) {
      update_post_meta($inserted_post_id,$key,$value[0]);
    }
    set_post_thumbnail($inserted_post_id, $inserted_thumbnail);

    restore_current_blog(); // return to original blog
}

}
add_action( 'save_post', 'copy_across_to_multisite' );

This works great, but it doesn't bring across the featured image; I'm assuming because I need to also move the featured image into the upload folder for that multisite? The image does come across in the media library (albeit with no thumbnail and linking back to the other site - which I don't mind) but doesn't 'attach' onto the post and show up as a featured image.

Can anyone help out with this? Has anyone done anything similar? Thanks

1

1 Answers

3
votes

Well, finally managed to (pretty much) cobble together a solution.

So in case anyone needs a similar function, it will take a little tweaking depending on your situation (changing the number in the switch_to_blog function as needed).

function copy_across_to_multisite( $post_id ) {

// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
    return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
    return $post_id;

if ($post_id && get_current_blog_id() == 1) {
    $post = get_post($post_id, ARRAY_A); // get the original post
    if ($post->post_status['trash'] || $post['post_status'] == 'auto-draft') {
        return $post_id;
    }
    $meta = get_post_meta($post_id);
    $post_thumbnail_id = get_post_thumbnail_id($post_id);
    $image_url = wp_get_attachment_image_src($post_thumbnail_id, 'full');
    $image_url = $image_url[0];

    $post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post

    switch_to_blog(2); // switch to target blog
    $inserted_post_id = wp_insert_post($post); // insert the post
    foreach($meta as $key=>$value) {
      update_post_meta($inserted_post_id,$key,$value[0]);
    }

    // Add Featured Image to Post
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents($image_url); // Get image data
    $filename   = basename($image_url); // Create image file name

    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir['path'] ) ) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }

    // Create the image  file on the server
    file_put_contents( $file, $image_data );

    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );

    // Set attachment data
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title'     => sanitize_file_name( $filename ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );

    // Include image.php
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );

    // And finally assign featured image to post
    set_post_thumbnail( $inserted_post_id, $attach_id );

    restore_current_blog(); // return to original blog
}

}
  add_action( 'save_post', 'copy_across_to_multisite' );