WordPress at a glance

wp_send_json() WP 1.0

Send a JSON response back to an Ajax request.

Works based on: wp_json_encode()

No Hooks.

Return

Null. Nothing.

Usage

wp_send_json( $response, $status_code );
$response(mixed) (required)
Variable (usually an array or object) to encode as JSON, then print and die.
$status_code(int)
The HTTP status code to output.
Default: null

Changelog

Since 3.5.0 Introduced.
Since 4.7.0 The $status_code parameter was added.

Code of wp send json: wp-includes/functions.php WP 5.2.2

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

Related Functions

From tag: AJAX

More from category: Helper Functions

No comments
    Hello, !     Log In . Register