Skip to content

Commit c64b52c

Browse files
authored
gpdtc-recalc.php: Migrated from experimental folder.
1 parent e21b59b commit c64b52c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Date Time Calculator // Recalculate Field Value on View
4+
* https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/
5+
*
6+
* Experimental Snippet 🧪
7+
*
8+
* Recalculate a calculated field's value every time it's viewed.
9+
*
10+
* Works great with Date Time Calculator's [:age modifier][1].
11+
*
12+
* [1]: https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/#calculating-age
13+
*/
14+
add_action( 'wp_loaded', function() {
15+
16+
$form_id = 123; // Change this to the form's ID
17+
$field_id = 4; // Change this to the Calculation field's ID.
18+
19+
$values = array();
20+
21+
add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values ) {
22+
if ( $field['id'] !== $field_id ) {
23+
$values[ $field['id'] ] = $value;
24+
return $value;
25+
}
26+
$form = GFAPI::get_form( $entry['form_id'] );
27+
$_entry = $entry + $values;
28+
return GFCommon::calculate( $field, $form, $_entry );
29+
}, 10, 4 );
30+
31+
} );

0 commit comments

Comments
 (0)