Zipping up attached images from taxonomy & date Zipping up attached images from taxonomy & date wordpress wordpress

Zipping up attached images from taxonomy & date


WordPress will return the full URL to each image, therefore you will have to convert those URLs to internal PATHS in order to add them to your ZIP file.

Here's how you would convert WordPress URLs to internal server paths:

function convert_upload_url_to_path($url) {    $path = $url;    $upload_dir_infos = wp_upload_dir();    $upload_url = $upload_dir_infos['baseurl']; // http://example.com/wp-content/uploads     $upload_path = $upload_dir_infos['basedir']; // C:\path\to\wordpress\wp-content\uploads     // Step 1: remove $upload_url    $path = str_replace($upload_url, '', $path);    // Step 2: prepend $upload_path    $path = $upload_path.$path;    return $path;}

Now in order to get the actual URL from each attachement image, use the wp_get_attachment_image_src($attachment_id, $size); function.

The first parameter is the attachment ID, and the second (optional) is the image size wanted. It will return the URL to the image.

Here is an example using your $atts_ids variable:

$files_to_zip = array();foreach ($atts_ids as $attachement_id) {    $image_info = wp_get_attachment_image_src($attachement_id, 'full');    $image_url = $image_info[0];    $image_path = convert_upload_url_to_path($image_url);    $files_to_zip[] = $image_path;}// zip 'em!$result = create_zip($files_to_zip,'vip-download.zip');

Note that specifying the $size parameter will not resize the image for you. Instead, it will return the nearest size already available. If you would like to retrieve the original image uploaded in its biggest size just specify 'full'. You can set WordPress to resize uploaded images (done when upload completes) by setting your desired custom size in the WordPress Dashboard > Settings > Media.