Ask your WordPress questions! Pay money and get answers fast! (more info)

attach unzip images to post

  • REFUNDED

hi, i have a csv file (that i want import in wp) where contain a huge list of post, every post have a link file with zip format where are contained the images for featured image and attachment (10 images for every post), i want create a script for extract the images for every zip and then import it for every post;

i want you create a script that take the zip file from a custom field (zip external URL) of each post, and then assign the images unzipped to the post


i tried to search some guide online and i have found some similar question:

http://wordpress.stackexchange.com/questions/46799/attach-unzipped-files-to-wordpress

http://wpquestions.com/Attach_unzip_files_to_post/9636

or this plugin https://wordpress.org/plugins/upload-media-by-zip/

this plugin are similar to my requirement , the only problem of this is
- require to upload zip file from media uploader and not from external URL
- unzip only 1 zip-post at time.

A better solution for start this script is with "Custom Bulk Action" like https://www.skyverge.com/blog/add-custom-bulk-action/


thanks

Answers (1)

2017-02-06

Francisco Javier Carazo Gil answers:

Good morning Markus,

Firstly you have to include in your bulk import something like:

$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
$zip->extractTo('/myzips/extract_path/');
$zip->close();
echo 'woot!';
} else {
echo 'doh!';
}

Then, you have to find in the extract path (that should be different in every post) the different images:
$directory = "mytheme/images/myimages";
$images = glob($directory . "*.jpg");

Now you can loop over images.


Francisco Javier Carazo Gil comments:

How to loop?

foreach($images as $filename)
{
$wp_filetype = wp_check_filetype(basename($filename), null );

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

$attach_id = wp_insert_attachment( $attachment, $uploadfile );

$imagenew = get_post( $attach_id );
$fullsizepath = get_attached_file( $imagenew->ID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data );

}

}

Finally you could set the post thumbnail in this way:

<?php set_post_thumbnail( $post, $thumbnail_id ); ?>

And that's all!


markus88 comments:

hi, can you write me a full code that work ?

your information are helpful but
I already knew how to implement this script, but I wanted to use wpquestions to have an already working code


Francisco Javier Carazo Gil comments:

Sorry but for $5 this is not possible.

You are asking for a complete plugin working and the cost is bigger.


markus88 comments:

if you customize this plugin https://wordpress.org/plugins/upload-media-by-zip/ the code to make is less;

in any case if you can not do it, I am waiting for proposals from other developers, thanks anyway