A WordPress-centric search engine for devs and theme authors



wp_send_json ›

Since3.5.0
Deprecatedn/a
wp_send_json ( $response, $status_code = null )
Parameters: (2)
  • (mixed) $response Variable (usually an array or object) to encode as JSON, then print and die.
    Required: Yes
  • (int) $status_code The HTTP status code to output.
    Required: No
    Default: null
Defined at:
Codex:
Change Log:
  • 4.7.0

Send a JSON response back to an Ajax request.



Source

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