I would like to offer my visitors the option to download the entire photo gallery (displayed on dedicated pages) as a ZIP file displayed at the bottom of each gallery page. – The full-sized image will have to be included.
David Walsh has given some code in his post here to zip up files but I’m having troubles integrating that with WordPress functions.
I’m aware there is a NextGEN gallery download plugin but I’m not in a position to use that as I use the native wordpress gallery functions.
A similar question with an alternative (manual method) of completing the above can be found here: Plugin to download attached media files?
Any help would be greatly appreciated.
Thanks.
First you have to get the images. How to get all images of a gallery is described here.
WordPress uses two classes for unzipping files. The PHP bilt in
ZipArchive()
(usage see David Walsh). And PclZip, you can found this class inwp-admin/includes/class-pclzip.php
. If you got problems withZipArchive()
try the PclZip class.Now you just to have to glue both together. Maybe I can post some sample code later, currently I am not at my desk.
Update
Your question can be splitted into two parts. The first one is getting all images from a gallery. The second one is zipping the images and send the zip file.
I will only explain the first part, getting all images of a gallery, because zipping the files is slightly offtopic.
Maybe there are other solutions, but in this example I replace the original gallery shortcode with a custom one to get the images. The reason is, WordPress changed the galleries in v3.5 a bit.
Before 3.5, the images for a gallery are attachments of the post. After 3.5, the images are passed to the shortcode as an attribute. Since WP3.5 we can not longer get the attached images of a post, we have to fetch the list from the shortcode attributes. My strategy is to replace the original shortcode with a custom shortcode, grab the attributes and call the original shortcode to get the gallery output.
All gallery related things are within a class. To create a zip file, we can use another class that takes as input the output of the gallery class. Let’s start with a class and a simple constructor.
We will call the method
get_instance()
later in the plugin with the hookplugins_loaded
. In the constructor, we remove the original shortcode and replace it with our custom shortcodegallery_zip_shortcode()
. Now we need the shortcode callbackThe first thing in this method is to get the post because we need the post ID. Than we include
wp-includes/media.php
, this file contains the callback function for the original gallery shortcode. Now we call a method to get an array with all images, create the gallery output by calling the original gallery callback, create a link and append the link to the gallery output. The images itself, respectively the pathes to the images, are stored in the class variable$images
, we need this array later.The class variable
$image
holds an entry for each post with an gallery, so we can use the function either on frontpage or in single view. Each entry contains an array for each gallery, because there can be more than one gallery in each post.The core of the plugin is the method to get the images from the shortcode.
At first we decide if it is a single post or a list of post IDs. If it is a list of post IDs, we handle a gallery from WP3.5+. After that, we have to handle the
exclude
attribute. After setup all varibles, we can finally get the images from the gallery. The retrived images will be pushed into the class var$images
for later use.This is the gold of the plugin. Simply setup an array with query arguments, get the attachments with
get_posts()
and walk over the retrieved attachments. To handle different sizes, we get the attachment image and strip of the url. From the attached file, we take the path and put it together with the filename. In the array$images
are now all images and their pathes from the gallery.Basically your question is answered at this point. But you also want to createa zip file from the images. You could create a zip file from the array
$images
in the last method. But this method is called everytime a gallery is displayed and creating a zip file could take a while. Maybe noone would request the zip file you created here, this is a waste of resources.How can we do it better? Do you remember that I put all images in the class variable
$images
? We can use this class var for an ajax request. But an ajax request is just another page load and we can access the images only when the output of the gallery is created. We have to save our images in a place where we can access them even after a another page request.In this example I use a session variable to store the array with images. A session variable can be accessed even after another page reload. To store the images, I register a method with the
shutdown
hook. After WordPress finished rendering the page, theshutdown
hook will be called. At this point, we should have collected all images from all displayed galleries. We simply store the images and can access them in an ajax request.When the ajax request is triggered, we recall the session var and create a zip file from the data. But this is a bit off topic for this question.
I created a repository on GitHub with the complete plugin code. I hope it points you in the right direction.
I like the idea of Ralf’s plugin to be able to download a whole gallery in one go, but I haven’t been able to get it to work. I’ve come up with a workaround that works for our purposes. The method is to replace the native WP gallery with your own one which you place at the end of your theme’s
functions.php
file AND add the following file, nameddownload.php
into the active theme folder. In the custom gallery a link under the file calls the file download.php which automatically forces the download of your file to the hard drive. I’ve tested this on the latest Chrome, Firefox and Safari versions and it works fine. Have been using Twenty Twelve theme, but no reason why it shouldn’t work on other ones too.a) Add the following to the end of
functions.php
. This is simply taken from media.phpb) Copy and paste the following into a file called
download.php
in the theme’s base directory.c). Don’t forget to link to the file in the gallery!! Important!