【发布时间】:2018-01-04 23:55:58
【问题描述】:
我正在尝试创建一个 ajax 函数来检索电子邮件模板并发送它。到目前为止,这是我的代码:
<?php
add_action('wp_ajax_nopriv_test_function', 'test_function');
add_action('wp_ajax_test_function', 'test_function');
function test_function() {
$content = Array(
"name"=>$_POST['name'],
"email"=>$_POST['email'],
);
ob_start();
include("../emails/email_template.php");
$message = ob_get_contents();
ob_end_clean();
$headers[] = 'Content-Type: text/html; charset=UTF-8';
wp_mail($_POST['email'], "[E-mail subject] This is a test", $message, $headers);
$result['feedback'] = "All OK!";
}
wp_send_json($result);
die();
}
?>
- ajax 调用有效。我得到了正确的 response.feedback,即使知道它不是“一切正常!”。
- 你可以分析
email_template.phpby clicking here。它基本上是一个响应式电子邮件模板,从$contents接收一些PHP 变量。见第 313 行。
很遗憾,我收到了这个错误:
PHP Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0, referer: http://example.com/.
我在此处和其他来源中进行了搜索,但没有找到正确的答案。我错过了什么吗?这甚至可能吗?
提前致谢:)
【问题讨论】:
-
在这里分享 email_template.php 代码
-
刚刚更新了第2条。See code here.
标签: php ajax wordpress output-buffering