Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created November 7, 2024 10:13
Show Gist options
  • Save Lonsdale201/e22cb46ad9963e74aba9abb87b54653f to your computer and use it in GitHub Desktop.
Save Lonsdale201/e22cb46ad9963e74aba9abb87b54653f to your computer and use it in GitHub Desktop.
JetEngine - ACF Date Picker custom callback
// place the code in the child theme functions.php or a custom code snippets plugin, like FluentSnippets
// This code work very well with the ACF Date Picker field type. Will return the corrected date format
// You will see the new ACF Date option in the Filter Callback list
// You need to overwrite the date output format in the code
// https://www.advancedcustomfields.com
add_filter( 'jet-engine/listings/allowed-callbacks', 'add_acf_date_callback' );
add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'add_acf_date_callback_args', 10, 3 );
function add_acf_date_callback( $callbacks ) {
$callbacks['acf_date_format'] = 'ACF Date';
return $callbacks;
}
// Define the callback function for formatting the ACF date field
function acf_date_format( $acf_date ) {
if ( !empty( $acf_date ) ) {
$formatted_date = date( 'Y/m/d', strtotime( $acf_date ) ); // overwrite the Y/m/d with your own output date format if need
return $formatted_date;
}
return '';
}
// Add callback arguments
function add_acf_date_callback_args( $args, $callback, $settings = array() ) {
if ( 'acf_date_format' === $callback ) {
$args[] = isset( $settings['field_value'] ) ? $settings['field_value'] : '';
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment