Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active June 23, 2025 17:38
Show Gist options
  • Save Qubadi/9eb8c903aa3fba7adcf800a81c814669 to your computer and use it in GitHub Desktop.
Save Qubadi/9eb8c903aa3fba7adcf800a81c814669 to your computer and use it in GitHub Desktop.
JetEngine Dynamic field widget trim title/text+url link
Has developed a custom Trim Text with several new functionalities
( Dynamic field widget, Filter field output + Callback = JetEngine trim text) ,
including the ability to choose custom trim title/text for each device, and also toggle a link on the title/text.
Copy the following PHP and create a PHP snippet using your snippet plugins.
Paste the code into the plugin and save it.
____________________________________________
/**
* Description: JetEngine callback for responsive trimming with optional title link wrapper.
*/
if ( ! defined( 'ABSPATH' ) ) exit;
add_action( 'plugins_loaded', 'jetengine_tt_custom_init' );
function jetengine_tt_custom_init() {
if ( ! function_exists( 'jet_engine' ) ) return;
// 🔹 Register Callback Name
add_filter( 'jet-engine/listings/allowed-callbacks', function( $callbacks ) {
$callbacks['jetengine_tt_custom_cb'] = 'Jetengine Trim Text (Responsive)';
return $callbacks;
} );
// 🔹 Add Elementor UI controls
add_filter( 'jet-engine/listings/allowed-callbacks-args', function( $args ) {
$args['jetengine_tt_custom_len_desktop'] = array(
'label' => __( 'Desktop Trim Length (words)', 'jetengine' ),
'type' => 'number',
'default' => 20,
'label_block' => true,
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jetengine_tt_custom_cb' ),
),
);
$args['jetengine_tt_custom_len_tablet'] = array(
'label' => __( 'Tablet Trim Length (words)', 'jetengine' ),
'type' => 'number',
'default' => 15,
'label_block' => true,
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jetengine_tt_custom_cb' ),
),
);
$args['jetengine_tt_custom_len_mobile'] = array(
'label' => __( 'Mobile Trim Length (words)', 'jetengine' ),
'type' => 'number',
'default' => 10,
'label_block' => true,
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jetengine_tt_custom_cb' ),
),
);
$args['jetengine_tt_link_title'] = array(
'label' => __( 'Make Title Clickable', 'jetengine' ),
'type' => 'switcher',
'label_on' => __( 'Yes', 'jetengine' ),
'label_off' => __( 'No', 'jetengine' ),
'return_value' => 'yes',
'default' => '',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jetengine_tt_custom_cb' ),
),
);
$args['jetengine_tt_open_new_tab'] = array(
'label' => __( 'Open in New Tab', 'jetengine' ),
'type' => 'switcher',
'label_on' => __( 'Yes', 'jetengine' ),
'label_off' => __( 'No', 'jetengine' ),
'return_value' => 'yes',
'default' => '',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'jetengine_tt_custom_cb' ),
'jetengine_tt_link_title' => 'yes',
),
);
return $args;
} );
// 🔹 Pass callback args
add_filter( 'jet-engine/listing/dynamic-field/callback-args', function( $args, $callback, $settings = array() ) {
if ( 'jetengine_tt_custom_cb' === $callback ) {
$args[] = isset( $settings['jetengine_tt_custom_len_desktop'] ) ? absint( $settings['jetengine_tt_custom_len_desktop'] ) : 20;
$args[] = isset( $settings['jetengine_tt_custom_len_tablet'] ) ? absint( $settings['jetengine_tt_custom_len_tablet'] ) : 15;
$args[] = isset( $settings['jetengine_tt_custom_len_mobile'] ) ? absint( $settings['jetengine_tt_custom_len_mobile'] ) : 10;
$args[] = ! empty( $settings['jetengine_tt_link_title'] ) ? true : false;
$args[] = ! empty( $settings['jetengine_tt_open_new_tab'] ) ? true : false;
}
return $args;
}, 10, 3 );
// 🔹 Output trim CSS (with link color fix)
add_action( 'wp_head', 'jetengine_tt_custom_css', 100 );
}
function jetengine_tt_custom_css() {
echo '<style id="jetengine-tt-custom-css">
.jet-tt-desktop { display: inline; }
.jet-tt-tablet, .jet-tt-mobile { display: none; }
@media (max-width: 1024px) {
.jet-tt-desktop { display: none; }
.jet-tt-tablet { display: inline; }
}
@media (max-width: 767px) {
.jet-tt-tablet { display: none; }
.jet-tt-mobile { display: inline; }
}
/* Prevent title links from changing color or underline */
.jet-tt-desktop a,
.jet-tt-tablet a,
.jet-tt-mobile a {
color: inherit;
text-decoration: none;
}
</style>';
}
// 🔹 Callback logic
function jetengine_tt_custom_cb( $field_value = null, $desktop = 20, $tablet = 15, $mobile = 10, $link_title = false, $open_new_tab = false ) {
$field_value = wp_kses_post( $field_value );
$desktop_trim = wp_trim_words( $field_value, $desktop, '...' );
$tablet_trim = wp_trim_words( $field_value, $tablet, '...' );
$mobile_trim = wp_trim_words( $field_value, $mobile, '...' );
if ( $link_title && get_the_ID() ) {
$url = get_permalink( get_the_ID() );
$target = $open_new_tab ? ' target="_blank" rel="noopener noreferrer"' : '';
$desktop_trim = sprintf( '<a href="%s"%s>%s</a>', esc_url( $url ), $target, esc_html( $desktop_trim ) );
$tablet_trim = sprintf( '<a href="%s"%s>%s</a>', esc_url( $url ), $target, esc_html( $tablet_trim ) );
$mobile_trim = sprintf( '<a href="%s"%s>%s</a>', esc_url( $url ), $target, esc_html( $mobile_trim ) );
}
return sprintf(
'<span class="jet-tt-desktop">%s</span><span class="jet-tt-tablet">%s</span><span class="jet-tt-mobile">%s</span>',
$desktop_trim,
$tablet_trim,
$mobile_trim
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment