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