【问题标题】:Ajax function that retrieve an e-mail template and send it检索电子邮件模板并发送它的 Ajax 函数
【发布时间】: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();
}
?>
  1. ajax 调用有效。我得到了正确的 response.feedback,即使知道它不是“一切正常!”。
  2. 你可以分析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


【解决方案1】:

很抱歉将其发布为答案,但我没有足够的积分来发表评论。我已经关注这个问题好几个星期了,因为我对工作代码和非工作代码之间的关键区别非常好奇。我绝对不明白原始解决方案有什么问题。出于我的好奇,您能否在调用 ob_start() 之前添加以下代码

error_log( 'before ob_start(): ' . print_r( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), true ) );

只有在输出缓冲回调中调用 ob_start() 时才会出现您遇到的错误。我真的不明白这在你给定的代码中是如何发生的,但也许回溯会显示一些东西。

我知道几周过去了,也许你对这个问题失去了兴趣,但希望你和我一样,仍然想了解原因。这段代码我看了很多遍,看不出有什么问题,很想搞明白哪里出了问题。

提前感谢您的考虑。

更新: 我使用以下插件测试了您的代码:

<?php
/*
Plugin Name: Gruby's Problem
*/

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("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();
}

add_shortcode( 'send_gruby_ajax_test', function() {
?>
<button id="gruby_send_ajax_test">Send Gruby Ajax Test</button>
<script>
    jQuery( 'button#gruby_send_ajax_test' ).click( function() {
        jQuery.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', { action: 'test_function', name: 'Magenta', email: 'xxx@xxx.com' }, function( r ) {
            console.log( r );
        } );
    } );
</script>
<?php
} );
?>

我必须解决 2 个问题 - 在该行之后删除了一个无关的 '}'

$result['feedback'] = "All OK!";

并更改 email_template.php 中的第 313 行

Your order has shipped, <?php echo $content['nome'];?>!

Your order has shipped, <?php echo $content['name'];?>!

在这些更改之后,代码按预期工作。所以你的原始代码确实在我的环境中工作。我仍然很好奇你是如何让你的代码失败的。你能运行我的插件,看看它是否会导致和以前一样的错误。要测试插件,您需要创建一个包含帖子内容的页面:

[send_gruby_ajax_test]

这将显示一个按钮,单击该按钮将发送“test_function”的 AJAX 请求。

【讨论】:

  • 感谢您的关注,@magenta 我和您一样 :D 当我遇到问题时,我所有的时间都花在阅读代码并通过研究和(数百万次)测试尝试不同的解决方案上。在我的一项测试中,代码有效,但不是很干净。所以,最后,在重写之后,我做了一个和你很相似的代码。很高兴知道我不是唯一一个对代码充满热情的人。说真的,对我来说,编码就像游戏一样有趣。最好的问候!
【解决方案2】:

将我的代码更改为:

<?php
add_action('wp_ajax_nopriv_test_function', 'test_function');
add_action('wp_ajax_test_function', 'test_function');
function test_function() {

      ob_start();

        $content = Array(
            "nome"=>$_POST['nome_registro'],
            "email"=>$_POST['email_registro'],
        );?>

    <?php include(ABS_PATH_DEFINED_ELSEWHERE."my-plugin/inc/emails/email_template.php"); ?>

    <?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();
}
?>

而且工作就像一个魅力:)

有人解释了为什么之前的代码不起作用?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2017-01-05
    • 2015-11-11
    • 1970-01-01
    • 2012-05-17
    • 2019-09-06
    • 1970-01-01
    相关资源
    最近更新 更多