如何将Contact Form 7接口请求返回的变量传递到前端页面显示
原因说明
wpcf7_mail_sent 是Contact Form 7在邮件发送完成后触发的服务端后台钩子,执行时机在页面渲染流程结束之后,直接调用echo、var_dump、print等输出函数的内容不会被传递到前端页面,所以无法直接展示。
可行实现方案
方案1:将响应内容追加到表单提交成功提示中(最便捷)
利用Contact Form 7的反馈消息过滤器,把API返回的$body内容拼接到提交成功的提示里,用户提交表单后会直接在表单下方看到内容。
// 定义全局变量存储API响应内容 $custom_cf7_api_response = ''; add_action('wpcf7_mail_sent','Kiri_cf7_api_sender'); function Kiri_cf7_api_sender ( $contact_form) { global $custom_cf7_api_response; if ( $contact_form->title === 'Quote_form') { $submission = WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); $name = $posted_data["your-name"]; $surname = $posted_data["your-name2"]; $phone = $posted_data["tel-922"]; // 补全URL协议头,参数做urlencode转义避免请求错误 $url = "https://www.mytestapi.com?name=".urlencode($name)."&surname=".urlencode($surname)."&phone=".urlencode($phone); $response = wp_remote_post ($url); $body = wp_remote_retrieve_body( $response ); // 把响应内容赋值给全局变量 $custom_cf7_api_response = $body; // 原有日志逻辑保留 ob_start(); var_dump($name, $surname, $phone, $body); $contents = ob_get_contents(); ob_end_clean(); error_log($contents); } } } // 钩子修改表单提交反馈消息 add_filter( 'wpcf7_display_message', 'add_api_response_to_cf7_message', 10, 2 ); function add_api_response_to_cf7_message( $message, $status ) { global $custom_cf7_api_response; // 仅在提交成功状态且有API返回时追加内容 if ( $status === 'mail_sent' && !empty($custom_cf7_api_response) ) { $message .= '<br>API返回内容:<pre>' . esc_html($custom_cf7_api_response) . '</pre>'; } return $message; }
方案2:存储到session后在页面自定义位置输出
如果需要把API返回内容放到页面任意指定位置(比如表单外的其他区域),可以用session临时存储内容,再在对应模板位置输出。
// 第一步:开启session(如果站点没开启的话) add_action('init', 'start_cf7_custom_session'); function start_cf7_custom_session() { if( !session_id() ) { session_start(); } } // 修改原发送函数,把响应存到session add_action('wpcf7_mail_sent','Kiri_cf7_api_sender'); function Kiri_cf7_api_sender ( $contact_form) { if ( $contact_form->title === 'Quote_form') { $submission = WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); $name = $posted_data["your-name"]; $surname = $posted_data["your-name2"]; $phone = $posted_data["tel-922"]; $url = "https://www.mytestapi.com?name=".urlencode($name)."&surname=".urlencode($surname)."&phone=".urlencode($phone); $response = wp_remote_post ($url); $body = wp_remote_retrieve_body( $response ); // 存到session $_SESSION['cf7_api_response'] = $body; // 原有日志逻辑保留 ob_start(); var_dump($name, $surname, $phone, $body); $contents = ob_get_contents(); ob_end_clean(); error_log($contents); } } } // 第二步:在你需要展示内容的页面模板位置添加输出代码 // 放在主题模板对应位置即可: if (isset($_SESSION['cf7_api_response']) && !empty($_SESSION['cf7_api_response'])) { echo '<div class="api-response">API返回内容:<pre>' . esc_html($_SESSION['cf7_api_response']) . '</pre></div>'; // 展示完清空,避免重复显示 unset($_SESSION['cf7_api_response']); }
注意事项
- 输出API返回内容时建议用
esc_html()做安全转义,避免XSS风险 - 若API返回的是JSON格式,可以用
json_decode()解析后再格式化输出,可读性更强
内容的提问来源于stack exchange,提问作者KiriqN
相关产品推荐
相关产品推荐

