以重力形式计算年龄
Calculate Age in Gravity FForms
我正在尝试根据用户在 Gravity Form 中提供的出生日期计算年龄。现在的问题是我希望将年龄存储在重力形式的隐藏字段中,以便我可以在该隐藏字段上使用条件登录。
我已经为出生日期添加了一个日期选择器,但不确定我的函数是否可以计算准确的年龄,并且 return 它可以作为我可以在隐藏字段中使用的参数。
这是我写的代码:
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' 字段设置为等于您计算出的年龄然后 return表格。所以更新你的代码你会得到这样的东西:
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;
}
我正在尝试根据用户在 Gravity Form 中提供的出生日期计算年龄。现在的问题是我希望将年龄存储在重力形式的隐藏字段中,以便我可以在该隐藏字段上使用条件登录。
我已经为出生日期添加了一个日期选择器,但不确定我的函数是否可以计算准确的年龄,并且 return 它可以作为我可以在隐藏字段中使用的参数。
这是我写的代码:
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' 字段设置为等于您计算出的年龄然后 return表格。所以更新你的代码你会得到这样的东西:
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;
}