0

I want to populate dynamic data into a field in gravity form from function.php before form submissions. but my code below is not working.

My code/steps not working:

I am marking a gravity form field "Allow field to be populated dynamically" and giving a parameter name "time" So I can use it as a hook in function.php. Please see the screenshot https://snag.gy/AGQ8CU.jpg

and then, in my function.php, I am using this code to calculate current's time and passing it back to the form so the form can submit current time.

The code in function.php:

$timespam = "";

add_filter( 'gform_field_value_timestamp', 'timestamp_population_function');

function timestamp_population_function() {
    $timespam = time();
    return $timespam;
}

But my code is not working and not taking dynamic time from functions.php and sending current time.

I also want to re-use this dynamic value in a different function and also want to load another form's field value in below function. Such as:

add_filter( 'gform_field_value_hash', 'hash_population_function' );
function hash_population_function($value) {
    $x_amount = $_POST['input_26']; // I am trying to get value from form's field
    $hmac_data = $timespam . "^" . $x_amount;
    $x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);
    return $x_fp_hash;
}

Can you please check if my code/steps are good? or is there any other way to achieve it?

1 Answer 1

1

You have to add the $value parameter to your function and change the parameter value to match what you added in the form.

add_filter( 'gform_field_value_time', 'timestamp_population_function');

function timestamp_population_function($value) {
    $timespam = time();
    return $timespam;
}

You will only want to dynamically populate the current time. AFTER the form is submitted, run the following hook to take the current time from the form, and calculate the rest and then process it appropriately.

add_action( 'gform_after_submission_[form ID]', 'calc_post_data_[form ID]', 10, 2 );
function calc_post_data_15($entry, $form){
   $timespam = rgar($entry, '[form field ID for current time]');
   $x_amount =   rgar($entry, '[form field ID for amount]');
   $hmac_data = $timespam . "^" . $x_amount;
   $x_fp_hash = hash_hmac('MD5', $hmac_data, $transaction_key);

   return function_that_deals_with_this_result($x_fp_hash);
 }
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. I want to use this timestamp variable in different functions as well and want to pull another field value on the same function. I have updated my question
You need to take a value from another form after it is submitted and use it in the form with the timespam field?
i want to load the timespam dynamic value into the field and then i want to the use same timespam dynamic value into another function to calculate another dynamic field also, i want to get the same form's field value into the function for calculation and then submit the form
I will have to come back to this in a bit unless someone else answers first
the functionality i want to achieve is, i want to submit few dyanmic values from gravity form: current time, hash value. I have set them as hidden field in the form and gave them parameter name in the gravity form so i can calculate the dynamiv values in the functions.php. in functions.php, i want to calculate time so i can submit current time after form submissions and also, i want to calculate hash value whivh requires "current time" and "amount (which i will pull from gravity form's field)" and calculate the hash and then submit it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.