Recalculate Field Value on View

Experimental Snippet 🧪

Recalculate a calculated field’s value every time it’s viewed.

Works great with Date Time Calculator’s :age modifier.

Code

Filename: gpdtc-recalc.php

<?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 );

} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.