Best way to end WordPress ajax request and why?

Considering regular WordPress ajax requests like these:

add_action( 'wp_ajax_merrychristmas_happynewyear', array( $this, 'merrychristmas_happynewyear' ) );
add_action( 'wp_ajax_nopriv_merrychristmas_happynewyear', array( $this, 'merrychristmas_happynewyear' ) );

Will it be best to end function merrychristmas_happynewyear with die(), die(0), wp_die(), or something else and why?


Using wp_die() is the best of those options.

As others have noted, there are many reasons to prefer a WordPress-specific function over the plain die or exit:

  • It allows other plugins to hook into the actions called by wp_die().
  • It allows a special handler for exiting to be used based on context (the behavior of wp_die() is tailored based on whether the request is an Ajax request or not).
  • It makes it possible to test your code.

The last one is more important, which is why I added that note to the Codex. If you want to create unit/integration tests for your code, you will not be able to test a function that calls exit or die directly. It will terminate the script, like it is supposed to. The way that WordPress’s own tests are set up to avoid this (for the Ajax callbacks that it has tests for), is to hook into the actions triggered by wp_die() and throw an exception. This allows the exception to be caught within the test, and the output of the callback (if any) to be analyzed.

The only time that you would use die or exit is if you want to bypass any special handling from wp_die() and kill the execution immediately. There are some places where WordPress does this (and other places where it might use die directly just because the handling from wp_die() is not important, or nobody has attempted to create tests for a piece of code yet, so it was overlooked). Remember that this also makes your code more difficult to test, so it would generally only be used in code that isn’t in a function body anyway (like WordPress does in admin-ajax.php). So if the handling from wp_die() is specifically not desired, or you are killing the script at a certain point as a precaution (like admin-ajax.php does, expecting that usually an Ajax callback will have already properly exited), then you might consider using die directly.

In terms of wp_die() vs wp_die( 0 ), which you should use depends on what is handling the response of that Ajax request on the front end. If it is expecting a particular response body, then you need to pass that message (or integer, in this case) to wp_die(). If all it is listening for is the response being successful (200 response code or whatever), then there is no need to pass anything to wp_die(). I would note, though, that ending with wp_die( 0 ) would make the response indistinguishable from the default admin-ajax.php response. So ending with 0 does not tell you whether your callback was hooked up properly and actually ran. A different message would be better.

As pointed out in other answers, you will often find wp_send_json() et al. to be helpful if you are sending a JSON response back, which is generally a good idea. This is also superior to just calling wp_die() with a code, because you can pass much more information back in a JSON object, if needed. Using wp_send_json_success() and wp_send_json_error() will also send the success/error message back in a standard format that any JS Ajax helper functions provided by WordPress will be able to understand (like wp.ajax).

TL;DR: You should probably always use wp_die(), whether in an Ajax callback or not. Even better, send information back with wp_send_json() and friends.

Leave a Comment