I'm trying to calculate the age from the date of birth provided by user in Gravity Form. Now the thing is that I want the age to be stored in a hidden field in the gravity form so that I can use conditional login on that hidden field.
I've added a datepicker for the date of birth, but not sure if my function is ok to calculate the exact age and return it to a parameter which I can use in a hidden field.
Here is the code I've written:
add_filter("gform_field_value_age", "_populate_age");
function _populate_age( $result, $value, $form, $field ){
$age = date('Y') - substr($value, 6, 4);
return $age;
}
I know my function is wrong but I was hoping someone can help me out on this.
Your first problem is there's no gform_field_value_age
filter available in Gravity Forms. You probably need to use gform_after_submission
(depends somewhat on what else you're trying to do) then in your function you need to set the hidden 'age' field equal to your calculated age then return the form. So updating your code you'd get something like:
add_filter("gform_field_value_age", "_populate_age");
function _populate_age( $entry, $lead, $field, $form ) {
$age = date('Y') - substr($entry[1], 10, 4);
//change the 1 in $entry[1] to birthday field id
$entry[2] = $age;
//$entry[2] the 2 should be the field id of the hidden age field
return $form;
}