【问题标题】:Calculate Age in Gravity FForms以重力形式计算年龄
【发布时间】:2016-03-19 19:25:25
【问题描述】:

我正在尝试根据用户在 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;

}

我知道我的功能有问题,但我希望有人能帮我解决这个问题。

【问题讨论】:

    标签: function wordpress gravity-forms-plugin


    【解决方案1】:

    您的第一个问题是重力形式中没有可用的gform_field_value_age 过滤器。您可能需要使用gform_after_submission(在某种程度上取决于您要尝试执行的其他操作),然后在您的函数中,您需要将隐藏的“年龄”字段设置为等于您计算的年龄,然后返回表单。所以更新你的代码你会得到类似的东西:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      相关资源
      最近更新 更多