Skip to content

Instantly share code, notes, and snippets.

@liranop
Last active September 19, 2023 10:46
Show Gist options
  • Select an option

  • Save liranop/23b0525475f66ed1fbfedc3a661ce0ab to your computer and use it in GitHub Desktop.

Select an option

Save liranop/23b0525475f66ed1fbfedc3a661ce0ab to your computer and use it in GitHub Desktop.
numeric duration callback to literal (dynamic field widget - elementor + jetengine)
add_filter( 'jet-engine/listings/allowed-callbacks', 'jet_custom_callbacks' );
/**
* Add callback to list.
*/
function jet_custom_callbacks( $callbacks ) {
$callbacks['jet_number_to_duration'] = __( 'Number to duration', 'txt-domain' );
return $callbacks;
}
/**
* Callback function for name.
*/
function jet_number_to_duration( $duration ) {
// Check if $duration is numeric or a valid numeric string
if ( is_numeric( $duration ) || preg_match('/^\d+(\.\d+)?$/', $duration) ) {
// Convert $duration to a float
$duration = (float)$duration;
// Calculate hours and minutes
$hours = floor( $duration );
$minutes = round(($duration - $hours) * 60);
// Build the duration string
$duration_string = '';
if ($hours > 0) {
$duration_string .= $hours . ' ' . _n( 'hour', 'hours', $hours, 'txt-domain' );
}
if ($minutes > 0) {
if ($duration_string !== '') {
$duration_string .= ' ';
}
$duration_string .= $minutes . ' ' . _n( 'minute', 'minutes', $minutes, 'txt-domain' );
}
return $duration_string;
}
// If $duration is not numeric or empty, return it as is
if (!empty($duration)) {
return $duration;
}
// If $duration is empty, return an empty string
return '';
}
@liranop

liranop commented Sep 18, 2023

Copy link
Copy Markdown
Author

Screen Shot 2023-09-18 at 22 55 29

Screen Shot 2023-09-18 at 22 55 49

@alfredo49

alfredo49 commented Sep 19, 2023

Copy link
Copy Markdown

Works perfect thxs!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment