在多站点上复制Wordpress特色图像

时间:2013-11-21 21:12:24

标签: php wordpress

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

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

这很有效,但它没有带来特色图像;我假设是因为我还需要将特色图像移动到该多站点的上传文件夹中?图像确实在媒体库中出现(虽然没有缩略图并链接回到其他网站 - 我不介意)但是没有“附加”到帖子上并显示为特色图片。

任何人都可以帮忙吗?有没有人做过类似的事情?感谢

1 个答案:

答案 0 :(得分:1)

好吧,最终设法(几乎)凑齐了一个解决方案。

因此,如果任何人需要类似的功能,根据您的具体情况需要稍微调整(根据需要更改switch_to_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);
    $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' );