Enhance your JetEngine-powered WordPress website by creating dynamic ZIP file download links for attachments. This code snippet enables you to group multiple files into a ZIP archive, customize file naming conventions, and generate secure download links—all within JetEngine listings. Perfect for websites requiring bulk file downloads!
Create a media field in your CCT, CPT, Option Pages, or Meta Boxes, and set the value format of the field to "Media ID.
Add a media field in JetFormBuilder, set the Field Value to "Attachment ID," and configure the number of files allowed for upload and the total file size limit.
Add the code snippet provided below to your Code Snippets plugin or the functions.php file of your theme. This enables the option to generate a ZIP file for attachments.
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die(); }
add_filter( 'jet-engine/listings/allowed-callbacks', 'jet_engine_add_attachment_zip_callback', 10, 2 );
add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'jet_engine_add_attachment_zip_callback_args', 10, 3 );
add_filter( 'jet-engine/listings/allowed-callbacks-args', 'jet_engine_add_attachment_zip_callback_controls' );
function jet_engine_add_attachment_zip_callback( $callbacks ) {
$callbacks['jet_engine_get_attachment_zip_link'] = 'Get attachment ZIP file link';
return $callbacks;
}
function jet_engine_get_attachment_zip_link( $attachment_id, $display_name = 'file_name', $label = '' ) {
// Ensure the attachment IDs are in an array format
if ( ! is_array( $attachment_id ) ) {
$attachment_id = explode( ',', $attachment_id );
}
$zip = new ZipArchive();
$upload_dir = wp_upload_dir(); // WordPress uploads directory
$zip_file = tempnam( $upload_dir['path'], 'attachments_' ) . '.zip';
if ( $zip->open( $zip_file, ZipArchive::CREATE ) !== TRUE ) {
return '<p>' . esc_html__( 'Could not create ZIP file.', 'jet-engine' ) . '</p>';
}
foreach ( $attachment_id as $key => $value ) {
$file_data = \Jet_Engine_Tools::get_attachment_image_data_array( $value, 'all' );
if ( ! empty( $file_data['url'] ) ) {
$file_path = get_attached_file( $value );
if ( file_exists( $file_path ) ) {
$file_extension = pathinfo( $file_path, PATHINFO_EXTENSION );
$file_name = basename( $file_path );
// Handle dynamic file naming based on display_name option
switch ( $display_name ) {
case 'post_title':
$file_name = get_the_title( $value ) . '.' . $file_extension;
break;
case 'custom':
$file_name = $label . '_' . $key . '.' . $file_extension;
break;
default:
// Use the original file name
break;
}
// Add the file to the ZIP archive
$zip->addFile( $file_path, $file_name );
} else {
error_log( "File does not exist: {$file_path}" );
}
} else {
error_log( "Invalid file data for attachment ID: {$value}" );
}
}
$zip->close();
// Generate the public URL for the ZIP file
$zip_url = str_replace( ABSPATH, site_url( '/' ), $zip_file );
return sprintf( '<p><a href="%1$s" download>%2$s</a></p>', esc_url( $zip_url ), esc_html__( 'TD - EID', 'jet-engine' ) );
}
function jet_engine_add_attachment_zip_callback_args( $args, $callback, $settings = array() ) {
if ( 'jet_engine_get_attachment_zip_link' === $callback ) {
$args[] = isset( $settings['jet_attachment_name'] ) ? $settings['jet_attachment_name'] : 'file_name';
$args[] = isset( $settings['jet_attachment_label'] ) ? $settings['jet_attachment_label'] : '';
}
return $args;
}
function jet_engine_add_attachment_zip_callback_controls( $args = array() ) {
$args['jet_attachment_name'] = array(
'label' => esc_html__( 'Display name', 'jet-engine' ),
'type' => 'select',
'label_block' => true,
'description' => esc_html__( 'Select attachment name format to display', 'jet-engine' ),
'default' => 'file_name',
'options' => array(
'file_name' => 'File name',
'post_title' => 'Attachment post title',
'custom' => 'Custom',
),
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_zip_link' ),
),
);
$args['jet_attachment_label'] = array(
'label' => esc_html__( 'Custom label', 'jet-engine' ),
'type' => 'text',
'label_block' => true,
'description' => esc_html__( 'Set custom text for the attachment link', 'jet-engine' ),
'default' => '',
'condition' => array(
'jet_attachment_name' => 'custom',
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_zip_link' ),
),
);
return $args;
}
Join To Get Updates
Your trusted partner in learning and growth. Together, let’s unlock your true potential.