I've been stuck on this topic for a while. I followed Pippin's tutorial . According to a 2nd wordpress developer who I got advice from, this tutorial is now out of date/ not appropriate.

He told me that I shouldn't echo things in the AJAX callback.

JavaScript should handle that. Also to send data in your callback, use wp_send_json() or the higher level functions wp_send_json_success() and wp_send_json_error(). All of them will die for you. No need to exit or die afterward, like Pippin has done it in his tutorial.

Which developer is correct? In the Codex, the example uses echo in the callback, just like Pippin has in his tutorial.

Please can someone clarify? I've been getting conflicting advice & I'm confused by this topic.

  • Matt, you have asked the same question three times. I'm not going to close them as duplicates, but please, for future posts: Stick with one question. – kaiser Mar 10 '14 at 18:52
  • Btw, Matt: Please read the license in the footer. You can't just copy/paste stuff from answers or questions on this site without attribution. – kaiser Mar 11 '14 at 0:08
  • Thanks for the feedback. I'll keep both those points in mind! – mattnewbie Mar 13 '14 at 15:42


Both ways are okay, as long as you die() your AJAX-function somehow.

I take it for granted that you handle the security well - this is important for both methods.

The Long Tail

It actually does not really matter which approach you use, and it depends a great deal on what your AJAX-Request does.

The most important thing is that you use a function that does the die() for you, or you put it directly in your AJAX function.

There are two scenarios I can think of on top of my head:

Simple Content Request

You do a simple content request (loading an image for example): in this case you could echo the response directly in your AJAX function, so you can just insert the HTML after your AJAX-call.

If you output <img src="http://url.com/test.jpg" /> in AJAX, you can just use it like that:

success: function(data) {

Do not forget to put the die() in your AJAX function!

Complex/Multiple Results

On the other hand, if you got multiple results or structured data in your callback, the better way would be to use wp_send_json( $results ).

The first reason is that you can pass an array or an object directly to the function, without having to handle the encoding and structuring.

The second reason is the way you can process the data in your success-function.

Examples for this would be loading new posts, automatically refreshing a few parts of your site (like a live score), and stuff like that.

  • Thank-you, that makes sense! A cheeky Q if I may, for the complex/ multiple results way, do you know of a good tutorial? I'm relatively new to coding & wordpress. There seem to be many out of date/ incorrect tutorials around, & it's hard to navigate through them if you're a novice like I am. Thanks! – mattnewbie Mar 6 '14 at 18:03
  • Hm, this is more of a general jQuery/AJAX thing. You use WordPress just to calculate the results. after you send out the results as an Array or Object via JSON, it's all about jQuery and Javascript coding. I do not know of any good tutorial by now. – fischi Mar 7 '14 at 15:06

Well, you echo it in php and that's what's returned to the javascript. Think of it this way, you're initiating a new request to the server and what you get back is the output of that request, i.e. the echo. That will work whether or using WordPress or not. I wouldn't consider it incorrect or bad practice to do just this.

Those "higher level" functions are excellent helper functions and it's generally best to use core WordPress functions where appropriate. But it's more important to use things like nonces and check referrers than to make sure you use helper functions instead of echoing, encoding and dieing yourself.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.