Last active
August 25, 2025 07:52
-
-
Save wpmudev-sls/335de2453c2630196017441a08e9f259 to your computer and use it in GitHub Desktop.
[Forminator] - Custom form submission date format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator] - Custom form submission date format (Rev.1) | |
* Description: This MU Plugin allows the usage of a custom date format for the Submission page | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-3998 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
// | |
// NOTE: This doesn't affect the Quizzes/Polls submission date format. That date format | |
// is taken from the Settings > General > Date Format section at the WP Admin. | |
// | |
add_action( 'plugins_loaded', function(){ | |
// >>> [ Snippet settings BEGIN ] ------------------------------------------------- | |
$new_format = 'd.m.Y @ g:i'; | |
// >>> [ Snippet settings END ] --------------------------------------------------- | |
add_filter( 'forminator_custom_form_entries_iterator', function( $iterator, $entry ) use ( $new_format ) { | |
$dates = array(); | |
if ( ! empty( $iterator['entry_date'] ) ) { | |
$dates[] = &$iterator['entry_date']; | |
} | |
if ( ! empty( $iterator['summary']['items'][1]['value'] ) ) { | |
$dates[] = &$iterator['summary']['items'][1]['value']; | |
} | |
if ( ! empty( $iterator['detail']['items'][0]['value'] ) ) { | |
$dates[] = &$iterator['detail']['items'][0]['value']; | |
} | |
foreach ( $dates as &$date ) { | |
try { | |
$new_date = \DateTime::createFromFormat( "M d, Y \@ g:i A", $date ); | |
} catch ( \Exception $e ) { | |
$new_date = null; | |
} | |
if ( null !== $new_date ) { | |
$date = $new_date->format( $new_format ); | |
} | |
} | |
return $iterator; | |
}, 10, 2 ); | |
if ( ! is_admin() || ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_forminator' ) ) ) { | |
return; | |
} | |
$nonce = ! empty( $_POST['_forminator_nonce'] ) | |
? sanitize_text_field( wp_unslash( $_POST['_forminator_nonce'] ) ) | |
: ''; | |
if ( ! wp_verify_nonce( $nonce, 'forminator_export' ) ) { | |
return; | |
} | |
add_filter( 'forminator_addon_slugs_from_entry_model', function( $addon_slugs, $entry_model, $addon_metadata_pattern_prefix ) { | |
$GLOBALS['_for_ee'][] = $entry_model; | |
return $addon_slugs; | |
}, 10, 3); | |
add_filter( 'forminator_custom_form_export_mappers', function( $mappers, $model_id, $model ) use ( $new_format ) { | |
$GLOBALS['_for_ee'] = array(); | |
$GLOBALS['_for_ee_i'] = 0; | |
$cloned_meta_key = $mappers[1]['meta_key'] ?? false; | |
if ( false === $cloned_meta_key ) { | |
return $mappers; | |
} | |
if ( ! empty( $mappers[0]['property'] ) && 'time_created' === $mappers[0]['property'] ) { | |
$mappers[0] = array( | |
'meta_key' => $cloned_meta_key, | |
'label' => esc_html__( 'Submission Time', 'forminator' ), | |
'type' => '%%DATE%%', | |
); | |
add_filter( 'forminator_entry_meta_value_to_string', function( $string_value, $field_type, $meta_value, $allow_html, $truncate ) use ( $new_format ) { | |
if ( '%%DATE%%' === $field_type ) { | |
if ( isset( $GLOBALS['_for_ee'][ $GLOBALS['_for_ee_i'] ] ) ) { | |
$entry = $GLOBALS['_for_ee'][ $GLOBALS['_for_ee_i']++ ]; | |
$date = date( $new_format, strtotime( $entry->date_created_sql ) ); | |
return $date ?? $entry->time_created; | |
} | |
return $string_value; | |
} | |
return $string_value; | |
}, 10, 5 ); | |
} | |
return $mappers; | |
}, 10, 3 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my case $new_date was false and the script crashed. I fixed it in my fork.