Skip to content

Instantly share code, notes, and snippets.

@LaxusCroco
Last active July 17, 2023 13:39
Show Gist options
  • Save LaxusCroco/ff5ba03d006a98943025b2ac1a463172 to your computer and use it in GitHub Desktop.
Save LaxusCroco/ff5ba03d006a98943025b2ac1a463172 to your computer and use it in GitHub Desktop.
JetEngine CPT admin callback download button for Media meta field (supports all value types)
<?php
add_filter( 'jet-engine/post-type/predifined-columns-cb-for-js', function( $callbacks ) {
$callbacks['jet_engine_custom_cb_download_button'] = array(
'description' => 'Download media file',
'args' => array(
'media' => array(
'label' => __( 'Set field', 'jet-engine' ),
'description' => __( 'Meta field to get media from', 'jet-engine' ),
'value' => '',
),
),
);
return $callbacks;
} );
function jet_engine_custom_cb_download_button( $post_id, $field ) {
$attachment_id = get_post_meta( $post_id, $field, $single = true );
if ( ! $attachment_id ) {
return null;
}
$attachment_url = \Jet_Engine_Tools::get_attachment_image_data_array( $attachment_id, 'url' )['url'] ?? '#';
$result = "<a href='" . esc_url( $attachment_url ) . "' download>Download</a>";
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment