Home Forums General Issues Copy Image / Media Uploads to Another Page Reply To: Copy Image / Media Uploads to Another Page

  • Thanks. I think I know what you mean. I played around for a few hours yesterday and I think I’m close, but not quite.

    This is what I have so far – I pass in the post to be copied to the new site and the target blog ID. It should work the same to copy the post on the same site if you were to remove the switch_to_blog() lines.

    public function copy_post_to_blog($post, $target_blog_id) {
                $source_blog_id = 1;
                switch_to_blog($source_blog_id); // Enforce that we're still on blog ID 1
                $meta = get_post_meta($post->ID);
                $post_custom = get_post_custom($post->ID);
                $save_post = $post;
                $post->ID = ''; // empty id field, to tell wordpress that this will be a new post
                switch_to_blog($target_blog_id); // switch to target blog
                $inserted_post_id = wp_insert_post($post); // insert the post
                $attached_images = array();
                foreach($meta as $key => $value) {
                    $attach_id = NULL;
                    if( strpos($key, 'sections_') === 0 ) {
                        $new_file_url = $main_site_url . '/wp-content/uploads/' . $post_media_attachment['file'];
                        $post_media_attachment = wp_get_attachment_metadata($value[0]);
                        $main_site_url = get_site_url( get_current_blog_id() );
                        $current_site_url = get_site_url( get_current_blog_id() );
                        $img_url = $main_site_url . '/wp-content/uploads/' . $post_media_attachment['file'];
                        $info       = pathinfo($img_url);
                        $file_name  = basename($img_url,'.' . $info['extension']);
                         // Get the upload directory for the current site
                        $upload_dir = wp_upload_dir();
                        // Make the path to the desired path to the new file we are about to create
                        if( wp_mkdir_p( $upload_dir['path'] ) ) {
                            $file = $upload_dir['path'] . '/' . $file_name .'.'. $info['extension'];
                        } else {
                            $file = $upload_dir['basedir'] . '/' . $file_name .'.'. $info['extension'];
                         $image_data = file_get_contents( $main_site_url . '/wp-content/uploads/' . $post_media_attachment['file'] );
                        // Add the file contents to the new path with the new filename
                        file_put_contents( $file, $image_data );
                        $attachment = array(
                            'post_mime_type' => 'image/jpeg',
                            'post_title'     => sanitize_file_name( $file_name ),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                            'post_excerpt'   => '',
                            'post_name'      => sanitize_file_name( $file_name ),
                            'guid'           => $file
                        // Attach the new file and its information to the database
                        $attach_id = wp_insert_attachment( $attachment, $file, $inserted_post_id );
                        // Include code to process functions below:
                        require_once(ABSPATH . 'wp-admin/includes/image.php');
                        // Define attachment metadata
                        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
                        wp_update_attachment_metadata( $attach_id, $attach_data );
                    $new_value = ( isset($attach_id) ) ? $attach_id : $value[0];
                    update_post_meta($inserted_post_id, $key, $new_value);

    I feel like I’m switching blogs too much, maybe that’s an issue…and the $new_value field, what I’m trying to do is if it’s one of my “sections_” ACF fields, save the image value where applicable or just the value itself to the new post.

    What I don’t like about it, besides it not working, is it’s not dynamic – what if there are other ACF fields later…if they aren’t starting with the name “sections_” it won’t get picked up. I don’t really know enough of what’s going on to make this better.

    Do you happen to have any working code or can you help with what I’ve got started?