Skip to content

Instantly share code, notes, and snippets.

@moxet
Created October 28, 2025 12:09
Show Gist options
  • Select an option

  • Save moxet/53ead2aea67d06ad0ef2b886fa8d7b1a to your computer and use it in GitHub Desktop.

Select an option

Save moxet/53ead2aea67d06ad0ef2b886fa8d7b1a to your computer and use it in GitHub Desktop.
This script support multi-attachment (gallery) field using dynamic field.
<?php
/**
* Plugin Name: JetEngine - Get attachment file links (multi)
* Description: Dynamic Field callback that supports single **and multiple** attachment IDs.
* Version: 1.2.0
*/
if ( ! defined( 'WPINC' ) ) { die(); }
add_filter( 'jet-engine/listings/allowed-callbacks', 'je_multi_attach_link_callback', 10, 2 );
add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'je_multi_attach_link_callback_args', 10, 3 );
add_filter( 'jet-engine/listings/allowed-callbacks-args', 'je_multi_attach_link_callback_controls' );
function je_multi_attach_link_callback( $callbacks ) {
$callbacks['jet_engine_get_attachment_file_link'] = 'Get attachment file link(s) by ID';
return $callbacks;
}
/**
* Accepts:
* - Single ID (int|string)
* - Array of IDs
* - Comma-separated IDs string: "12,34,56"
* - JSON-encoded array string: "[12,34,56]"
*/
function jet_engine_get_attachment_file_link( $attachment_value, $display_name = 'file_name', $label = '', $is_external = '', $separator = '<br>' ) {
// Normalize to array of IDs
$ids = [];
if ( is_array( $attachment_value ) ) {
$ids = $attachment_value;
} elseif ( is_string( $attachment_value ) ) {
$trim = trim( $attachment_value );
// JSON array?
if ( str_starts_with( $trim, '[' ) && str_ends_with( $trim, ']' ) ) {
$maybe = json_decode( $trim, true );
if ( is_array( $maybe ) ) {
$ids = $maybe;
}
}
// Comma-separated fallback
if ( empty( $ids ) ) {
$parts = array_filter( array_map( 'trim', explode( ',', $trim ) ) );
if ( $parts ) { $ids = $parts; }
}
} elseif ( is_numeric( $attachment_value ) ) {
$ids = [ $attachment_value ];
}
// If still empty but value looks like scalar, try as single
if ( empty( $ids ) && ! empty( $attachment_value ) ) {
$ids = [ $attachment_value ];
}
$target = '';
$is_external = filter_var( $is_external, FILTER_VALIDATE_BOOLEAN );
if ( $is_external ) { $target = ' target="_blank" rel="noopener"' ; }
$links = [];
foreach ( $ids as $id ) {
$id = is_numeric( $id ) ? intval( $id ) : 0;
if ( ! $id ) { continue; }
$url = wp_get_attachment_url( $id );
if ( ! $url ) { continue; }
switch ( $display_name ) {
case 'post_title':
$name = get_the_title( $id );
break;
case 'current_post_title':
$name = get_the_title( get_the_ID() );
break;
case 'parent_post_title':
$parent_id = wp_get_post_parent_id( $id );
if ( ! $parent_id ) { $parent_id = get_the_ID(); }
$name = get_the_title( $parent_id );
break;
case 'custom':
$name = $label;
break;
default:
$name = basename( parse_url( $url, PHP_URL_PATH ) );
break;
}
$links[] = sprintf( '<a href="%1$s"%3$s>%2$s</a>', esc_url( $url ), esc_html( $name ), $target );
}
// Nothing found
if ( empty( $links ) ) { return ''; }
// Join by separator (default <br>)
return implode( $separator, $links );
}
function je_multi_attach_link_callback_args( $args, $callback, $settings = array() ) {
if ( 'jet_engine_get_attachment_file_link' === $callback ) {
$args[] = isset( $settings['jet_attachment_name'] ) ? $settings['jet_attachment_name'] : 'file_name';
$args[] = isset( $settings['jet_attachment_label'] ) ? $settings['jet_attachment_label'] : '';
$args[] = isset( $settings['jet_attachment_is_external'] ) ? $settings['jet_attachment_is_external'] : '';
$args[] = isset( $settings['jet_attachment_separator'] ) ? $settings['jet_attachment_separator'] : '<br>';
}
return $args;
}
function je_multi_attach_link_callback_controls( $args = array() ) {
$args['jet_attachment_name'] = array(
'label' => esc_html__( 'Display name', 'jet-engine' ),
'type' => 'select',
'label_block' => true,
'default' => 'file_name',
'options' => array(
'file_name' => 'File name',
'post_title' => 'Attachment post title',
'current_post_title' => 'Current post title',
'parent_post_title' => 'Parent post title',
'custom' => 'Custom',
),
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_file_link' ),
),
);
$args['jet_attachment_label'] = array(
'label' => esc_html__( 'Custom label', 'jet-engine' ),
'type' => 'text',
'label_block' => true,
'default' => '',
'condition' => array(
'jet_attachment_name' => 'custom',
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_file_link' ),
),
);
$args['jet_attachment_is_external'] = array(
'label' => esc_html__( 'Open in new window', 'jet-engine' ),
'type' => 'switcher',
'default' => '',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_file_link' ),
),
);
$args['jet_attachment_separator'] = array(
'label' => esc_html__( 'Links separator', 'jet-engine' ),
'type' => 'text',
'label_block' => true,
'default' => '<br>',
'description' => esc_html__( 'String to place between links (e.g., ", ", " | ", or "<br>")', 'jet-engine' ),
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jet_engine_get_attachment_file_link' ),
),
);
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment