通过多站点复制Wordpress特色图片


Copy a Wordpress featured image over multisite

我有以下功能来复制在一个网站上创建的帖子,然后将其转移到特定的多站点博客:

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

这很好,但它不会带来特色图像;我假设是因为我还需要将特色图像移动到多站点的上传文件夹中?这张图片确实出现在媒体库中(尽管没有缩略图,并且链接回了另一个网站——我不介意),但并没有"附加"到帖子上,而是显示为特色图片。

有人能帮忙吗?有人做过类似的事情吗?感谢

终于(差不多)拼凑出了一个解决方案。

因此,如果有人需要类似的函数,则需要根据您的情况进行一些调整(根据需要更改switch_to_log函数中的数字)。

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