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

如何用PHP从Gravity Form提交内容生成带特定样式的HTML文件

如何用PHP从Gravity Form提交内容生成带特定样式的HTML文件?

嘿,你已经找对方向啦!用gform_notification钩子来实现完全可行,我帮你把代码补全并优化,还加了样式处理和特殊字段的兼容:

1. 完善HTML生成函数

这个函数负责构建带自定义样式的HTML内容,处理表单字段,并且把文件保存到指定目录:

function write_html($entry, $form) {
    // 设置HTML文件的保存路径,建议放在WP上传目录下,确保服务器有写入权限
    $save_dir = ABSPATH . '/wp-content/uploads/gravity-form-htmls/';
    
    // 如果目录不存在,自动创建
    if (!file_exists($save_dir)) {
        mkdir($save_dir, 0755, true);
    }
    
    // 生成唯一文件名,用表单条目ID+时间戳避免重复
    $file_name = 'submission_' . $entry['id'] . '_' . date('YmdHis') . '.html';
    $full_file_path = $save_dir . $file_name;

    // 构建带自定义样式的HTML结构
    $html_content = '<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>表单提交详情 #' . $entry['id'] . '</title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: "Microsoft Yahei", Arial, sans-serif; max-width: 900px; margin: 30px auto; padding: 0 20px; }
        .page-header { text-align: center; padding-bottom: 20px; border-bottom: 2px solid #2c3e50; margin-bottom: 30px; }
        .page-header h1 { color: #2c3e50; margin-bottom: 10px; }
        .submission-info { color: #666; font-size: 14px; }
        .form-field { margin-bottom: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 8px; }
        .field-label { font-weight: 600; color: #333; font-size: 16px; margin-bottom: 8px; display: block; }
        .field-value { color: #555; font-size: 15px; line-height: 1.8; }
        .field-value ul { margin-left: 20px; }
        .field-value a { color: #3498db; text-decoration: none; }
        .field-value a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="page-header">
        <h1>Gravity Form 提交详情</h1>
        <div class="submission-info">
            提交ID:#' . $entry['id'] . ' | 提交时间:' . date('Y-m-d H:i:s', strtotime($entry['date_created'])) . '
        </div>
    </div>';

    // 遍历表单所有字段,生成对应的HTML内容
    foreach ($form['fields'] as $field) {
        // 跳过隐藏字段和无值字段
        if ($field->type === 'hidden' || empty($entry[$field->id])) {
            continue;
        }

        $label = esc_html($field->label);
        $value = esc_html(gf_entry_field_value($entry, $field));

        // 处理特殊字段类型:文件上传、多选框、单选框
        if ($field->type === 'fileupload') {
            $file_url = $entry[$field->id];
            $value = '<a href="' . esc_url($file_url) . '" target="_blank">点击查看上传文件</a>';
        } elseif ($field->type === 'checkbox') {
            $selected_values = explode(', ', $entry[$field->id]);
            $value = '<ul>';
            foreach ($selected_values as $val) {
                foreach ($field->choices as $choice) {
                    if ($choice['value'] === $val) {
                        $value .= '<li>' . esc_html($choice['text']) . '</li>';
                        break;
                    }
                }
            }
            $value .= '</ul>';
        } elseif ($field->type === 'radio') {
            foreach ($field->choices as $choice) {
                if ($choice['value'] === $entry[$field->id]) {
                    $value = esc_html($choice['text']);
                    break;
                }
            }
        }

        $html_content .= '<div class="form-field">
            <span class="field-label">' . $label . ':</span>
            <div class="field-value">' . $value . '</div>
        </div>';
    }

    $html_content .= '</body></html>';

    // 写入HTML文件到指定路径
    file_put_contents($full_file_path, $html_content);

    // 返回文件完整路径,方便后续操作(比如添加到邮件附件)
    return $full_file_path;
}

2. 结合gform_notification钩子触发生成

把你的钩子代码补全,在表单提交后调用上面的函数生成HTML文件,还可以选择把HTML作为附件加到通知邮件里:

// 替换30为你的目标表单ID
add_filter( 'gform_notification_30', 'generate_and_attach_html', 10, 3 );
function generate_and_attach_html( $notification, $form, $entry ) {
    // 调用HTML生成函数
    $html_file = write_html($entry, $form);

    // 可选:如果需要把HTML文件作为附件添加到通知邮件,取消下面的注释
    if (file_exists($html_file)) {
        $notification['attachments'][] = $html_file;
    }

    return $notification;
}

几个关键注意事项

  • 权限问题:确保/wp-content/uploads/gravity-form-htmls/目录有服务器写入权限(一般设置为0755即可),否则无法保存文件。
  • 样式自定义:你可以完全修改<style>标签里的内容,调整成你需要的特定样式,比如匹配你的品牌风格。
  • 字段扩展:如果你的表单有其他特殊字段(比如下拉框、多行文本、日期等),可以参照代码里的单选/多选/文件上传的处理方式,添加对应的字段类型判断和值的格式化。
  • 邮件附件:如果不需要把HTML文件作为邮件附件,只需要生成文件存档,直接去掉$notification['attachments']相关的代码即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:54:37