Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpdtc-recalc.php: Migrated from experimental folder. #957

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions gp-date-time-calculator/gpdtc-recalc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Gravity Perks // Date Time Calculator // Recalculate Field Value on View
* https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/
*
* Experimental Snippet 🧪
*
* Recalculate a calculated field's value every time it's viewed.
*
* Works great with Date Time Calculator's [:age modifier][1].
*
* [1]: https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/#calculating-age
*/
add_action( 'wp_loaded', function() {

$form_id = 123; // Change this to the form's ID
$field_id = 4; // Change this to the Calculation field's ID.

$values = array();

add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values ) {
if ( $field['id'] !== $field_id ) {
$values[ $field['id'] ] = $value;
return $value;
}
$form = GFAPI::get_form( $entry['form_id'] );
$_entry = $entry + $values;
return GFCommon::calculate( $field, $form, $_entry );
}, 10, 4 );

} );
Loading