I'm trying to create an ajax function that retrieve an e-mail template and send it. This is my code so far:
<?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();
}
?>
email_template.php
by clicking here. It's basically a resposive e-mail template that receives some PHP variables from $contents
. See line 313.Unfortunately, I'm getting this error:
PHP Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0, referer: http://example.com/
.
I've searched here and in other sources, but didn't found an proper answer. Am i missing something? Is this even possible?
Thanks in advance :)
Sorry for posting this as an answer but I don't have enough points to post a comment. I have been following this question for weeks as I am extremely curious as to the critical difference between the working code and the non-working code. I absolutely don't understand what is wrong with the original solution. For my curiosity can you add the following code before the call to ob_start()
error_log( 'before ob_start(): ' . print_r( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), true ) );
The error you are getting should only occur if ob_start() is called in an output buffering callback. I really don't see how this can happen in your given code but maybe the backtrace will show something.
I know several weeks have passed and maybe you have lost interest in this problem but hopefully you are like me and still would like to understand why. I have looked at this code many times and cannot see any problem and would really like to understand what is wrong.
Thanks in advance for any consideration.
UPDATE: I tested your code using the following plugin:
<?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
} );
?>
I had to fix 2 problems - removed an extraneous '}' after the line
$result['feedback'] = "All OK!";
and changed line 313 in email_template.php
Your order has shipped, <?php echo $content['nome'];?>!
to
Your order has shipped, <?php echo $content['name'];?>!
After these changes the code worked as expected. So your original code does work in my environment. I am still curious as to how you are getting your code to fail. Can you run my plugin and see if it causes the same error as before. To test the plugin you need to create a page with post content:
[send_gruby_ajax_test]
This will display a button which when clicked will send the AJAX request for the 'test_function'.