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).
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:
- Ajax in WordPress: A Strategy For Error Codes
- Writing a Custom Protocol for Ajax Responses
- 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:
- success messages,
- 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:
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.
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.
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.