wp_send_json
Send a JSON response back to an Ajax request.
Description
Parameters (2)
- 0. $response (mixed)
- Variable (usually an array or object) to encode as JSON, then print and die.
- 1. $status_code — Optional. (null) =>
null
- The HTTP status code to output.
Usage
if ( !function_exists( 'wp_send_json' ) ) { require_once ABSPATH . WPINC . '/functions.php'; } // Variable (usually an array or object) to encode as JSON, // then print and die. $response = null; // The HTTP status code to output. $status_code = null; // NOTICE! Understand what this does before running. $result = wp_send_json($response, $status_code);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/functions.php
- function wp_send_json( $response, $status_code = null ) {
- @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
- if ( null !== $status_code ) {
- status_header( $status_code );
- }
- echo wp_json_encode( $response );
- if ( wp_doing_ajax() ) {
- wp_die( '', '', array(
- 'response' => null,
- ) );
- } else {
- die;
- }
- }