You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gform_notification钩子中获取带实际值的格式化文本?

解决Gravity Forms获取替换后通知内容的方法

在gform_notification钩子中,直接拿到的$notification['message']是带合并标签的原始内容,你可以用Gravity Forms内置的GFCommon::replace_variables()函数替换合并标签为实际提交的值,具体实现如下:

代码示例

add_filter( 'gform_notification', 'process_notification_content', 10, 3 );
function process_notification_content( $notification, $form, $entry ) {
    // 替换合并标签为用户实际提交的数据
    $processed_content = GFCommon::replace_variables(
        $notification['message'],
        $form,
        $entry,
        false, // 是否URL编码,无需则设为false
        false, // 是否转义HTML,保存文章内容建议设为false
        true,  // 是否将换行转为<br>,根据需求调整
        'text' // 内容格式,对应通知的text/html类型,按需修改
    );

    // 执行保存到文章的操作示例
    $post_args = array(
        'post_title'   => '表单提交记录 - 条目ID:' . $entry['id'],
        'post_content' => $processed_content,
        'post_status'  => 'publish',
        'post_type'    => 'post' // 也可改为自定义文章类型
    );
    wp_insert_post( $post_args );

    // 返回原始通知数据,不影响原有通知发送逻辑
    return $notification;
}

关键说明

  • GFCommon::replace_variables()是Gravity Forms核心函数,能精准替换所有表单字段的提交值
  • 函数最后一个参数$format要和你设置的通知格式匹配(text或html),避免格式错乱
  • 若无需修改原有通知发送逻辑,只需保留返回原始$notification即可,只处理自己的保存需求

内容的提问来源于stack exchange,提问作者vdavid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 05:59:57