我正在尝试计算用户在Gravity Form中提供的出生日期的年龄。现在问题是我希望将年龄存储在重力形式的隐藏字段中,以便我可以在该隐藏字段上使用条件登录。
我已经为出生日期添加了一个日期选择器,但不确定我的函数是否可以计算确切的年龄并将其返回到我可以在隐藏字段中使用的参数。
这是我写的代码:
add_filter("gform_field_value_age", "_populate_age");
function _populate_age( $result, $value, $form, $field ){
$age = date('Y') - substr($value, 6, 4);
return $age;
}
我知道我的功能是错的,但我希望有人可以帮我解决这个问题。
你的第一个问题是Gravity Forms中没有gform_field_value_age
过滤器。你可能需要使用gform_after_submission
(在某种程度上取决于你想要做的其他事情)然后在你的函数中你需要设置隐藏的'age'字段等于你计算的年龄然后返回表单。所以更新你的代码你会得到类似的东西:
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;
}