Last active
April 6, 2021 14:29
-
-
Save zoerooney/10e3d1247164fe701620 to your computer and use it in GitHub Desktop.
Gallery page using ACF PRO with a dynamically generated zip file download of all gallery images. More info/ tutorial here: http://zoerooney.com/blog/tutorials/wordpress-gallery-with-automated-zip-file-download/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Check the gallery field (created with ACF PRO) for images | |
$images = get_field('images'); | |
if( $images ) : | |
// If it does have images: | |
// Create variable for download file | |
// This uses a /downloads/ directory and the page title as the file name | |
$destination = 'downloads/' . sanitize_title( get_the_title() ) . '.zip'; | |
// Check if the file already exists (see tutorial for a cautionary note) | |
if ( file_exists( $destination ) ) { | |
// If it exists, print the download link | |
echo '<a href="' . esc_url( home_url( '/' ) ) . $destination . '" class="download-link" download>DOWNLOAD ALL</a>'; | |
} else { | |
// If the file doesn't already exist, create the file | |
$files = array(); | |
foreach( $images as $singlefile ) { | |
// create an array of the image files in the gallery | |
$files[] = get_attached_file( $singlefile['ID'] ); | |
} | |
if( count( $files ) ) { | |
// Check if there are files in the array (files exist) | |
// If there are files, create a zip file in the location specified | |
$zip = new ZipArchive(); | |
$zip->open( $destination, ZipArchive::CREATE ); | |
foreach( $files as $file ) { | |
// for every file in the array | |
if ( file_exists($file) ) { | |
// if the file actually exists, add it to the zip file | |
$new_filename = substr($file,strrpos($file,'/') + 1); | |
$zip->addFile( $file, $new_filename ); | |
} | |
} | |
// Once you've got all the files, close out the zip | |
$zip->close(); | |
// Then link to the file you just created | |
echo '<a href="' . esc_url( home_url( '/' ) ) . $destination . '" class="download-link" download>DOWNLOAD ALL</a>'; | |
} else { | |
// No images are found, display an error message | |
echo 'no files found'; | |
} | |
} ?> | |
<!-- Next, display the on-page gallery --> | |
<div class="gallery-grid"> | |
<?php foreach( $images as $image ) : | |
// For every image in the gallery, display the thumbnail linked to a larger image for a lightbox ?> | |
<a href="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" rel="gallery"> | |
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /> | |
</a> | |
<?php endforeach; ?> | |
</div><!-- / .gallery-grid --> | |
<?php else : | |
// If the gallery field is empty: ?> | |
<p>No images found.</p> | |
<?php endif; ?> |
Awesome share for ACF Pro!
Authors website no longer exists. Recreated here: https://www.tring-web-design.co.uk/2017/04/acf-gallery-automated-zip-file-download/
this no delete the file after download and no generate new zip again, no works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey the tutorial page/website is not working.