Home » Functions » wp_send_json

The wp_send_json function is a simple utility function in WordPress that helps developers send a JSON response back to the client. JSON (JavaScript Object Notation) is a lightweight data format that is often used to exchange data between servers and web applications.

This function can be used to return a JSON object or array as a response to an AJAX request, for example. It takes a single argument, which is the data that you want to send back. This data can be any valid JSON value, such as a string, number, object, or array.

The wp_send_json function also sets the Content-Type header to application/json, which tells the client that the response is in JSON format. This header is important because it ensures that the client knows how to handle the response.

Here’s an example usage code of wp_send_json function:

function my_ajax_callback() {
  $data = array(
    'name' => 'John Doe',
    'age' => 35,
    'email' => 'johndoe@example.com'
add_action( 'wp_ajax_my_ajax_callback', 'my_ajax_callback' );

In this example, we define a function called my_ajax_callback that returns an array of data. We then use the wp_send_json function to send this data back to the client as a JSON response. Finally, we use the add_action function to register this function as an AJAX callback in WordPress.

Overall, the wp_send_json function is a useful and simple way to send JSON responses in WordPress.

Learn More on WordPress.org

WordPress snippets using the wp_send_json function

Register an account to save your snippets or go Pro to get more features.