wp_send_json( mixed $response, int $status_code = null,  $flags )

Sends a JSON response back to an Ajax request.


Parameters

$response

(Required) Variable (usually an array or object) to encode as JSON, then print and die.

$status_code

(Optional) The HTTP status code to output.

Default value: null

$options

(Optional) Options to be passed to json_encode(). Default 0.


Source

File: 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;
	}
}


Changelog

Changelog
Version Description
5.6.0 The $options parameter was added.
4.7.0 The $status_code parameter was added.
3.5.0 Introduced.