Software, Development, and WordPress

WordPress Ajax Responses in JSON

WordPress Ajax responses are things that I’ve written about several times in the past (and it’s because I do a lot of work with Ajax in WordPress and because I often see other code that uses it in a variety of ways).

WordPress Ajax Responses: Implement a client-side call.

Implementing a call to the server from the client-side.

Of course, I’m not trying to set a definitive way to using WordPress, Ajax, JSON, and so on; otherwise, I wouldn’t spend my time writing posts about it, but there are things that I think are important to cover if for no other reason than to serve as reminders both to myself and those reading.

In the last year, I’ve written:

  1. Ajax in WordPress: A Strategy For Error Codes
  2. Writing a Custom Protocol for Ajax Responses
  3. An Example of the Ajax API

And sure, some of the above posts are a bit out of date. All the more reason to write updated content, right?

Since I recently finished a series on implementing custom protocols, I thought it important to also remember that there are already ways to send WordPress Ajax responses if you’re looking for something a bit simpler.

Custom WordPress Ajax Responses in JSON

When writing Ajax responses, the majority of the responses (honestly, I’d say all of them but I’m sure there are outliers that I’m missing) are going to fall into one of two categories:

  1. success messages,
  2. failure messages.

To that end, there are two ways to offer WordPress Ajax responses in JSON that make it really easy to send that type of information back to the client. Specifically, these functions are [appropriately] named:

  1. wp_send_ajax_success
  2. wp_send_ajax_error

Though you can read the linked documentation above, there are two simple examples below that show how to use the function along with any necessary comments.

Success Messages

In the following example, assume that there’s a JavaScript file making a request to a server-side function that includes the following the code.

The function is responsible for determining if a user exists in the WordPress system and, if it does, returning an Ajax response with the user ID:

Obviously, we’re still encoding the information in the format of JSON but we’re taking advantage of the WordPress API to send the response back to the server. And this makes for more readable, WordPress-based and fully compatible code.

Error Messages

But what if the user isn’t found? What’s the best way to respond to that type of request? We can still use the same strategy as above: We’ll send a response formatted as JSON using the built-in WordPress API functions.

There will be one addition: We’ll use a WP_Error object, as well.

Notice above that everything looks the same save for the implement of WP_Error. The thing worth noting about this particular call is that WP_Error accepts two arguments. I’ve arbitrarily chosen -1 as my error code.

For your implementation and your project, the error code may vary.

A Note About WordPress Ajax Responses

Note that the above examples are just snippets so it’s not something that you can just copy and paste and drop into a PHP file (and I don’t encourage that type of development anyway).

The point is to show another way to handle WordPress Ajax responses without the need to write some custom protocol.

However, given that these functions still send a message back to the browser in a certain format you could come up with a particular type of protocol, couldn’t you?

But that’s already been covered in some form.


  1. Joel Worsham

    For what reason are you json_encode() within wp_send_json_success() when the latter already encodes it?

    Oh also, above you have the functions initially listed as wp_send_ajax_success() and wp_send_ajax_success(), so ajax instead of json.

    Great article, thank you! I do love using this function to save a bit of time programming.

    • Tom

      Thanks for calling me out on that! That’s basically muscle memory, but I’ve gone ahead and updated it so that it uses an associative array ;).

      But you’re right — the functions are great. The new REST API functions offer some alternatives too which I hope to cover in the coming week or so.

Leave a Reply

© 2020 Tom McFarlin

Theme by Anders NorenUp ↑