wppaste
WordPress

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

Since
3.5.0, 4.7.0, 5.6.0
Source
wp-includes/functions.php:4540
Sends a JSON response back to an Ajax request.

Parameters

$responsemixed
Variable (usually an array or object) to encode as JSON, then print and die.
$status_codeintoptional
The HTTP status code to output. Default null.Default: null
$flagsintoptional
Options to be passed to json_encode(). Default 0.Default: 0

Uses · 8

Used by · 4

Source

function wp_send_json( $response, $status_code = null, $flags = 0 ) {	if ( wp_is_serving_rest_request() ) {		_doing_it_wrong(			__FUNCTION__,			sprintf(				/* translators: 1: WP_REST_Response, 2: WP_Error */				__( 'Return a %1$s or %2$s object from your callback when using the REST API.' ),				'WP_REST_Response',				'WP_Error'			),			'5.5.0'		);	} 	if ( ! headers_sent() ) {		header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );		if ( null !== $status_code ) {			status_header( $status_code );		}	} 	echo wp_json_encode( $response, $flags ); 	if ( wp_doing_ajax() ) {		wp_die(			'',			'',			array(				'response' => null,			)		);	} else {		die;	}}

History

Introduced in 3.5.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from none to never.verified against source
5.6.0
The $flags parameter was added.from the docblock
4.7.0
The $status_code parameter was added.from the docblock
3.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.