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
- wp_is_serving_rest_request()Determines whether WordPress is currently serving a REST API request.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- status_header()Sets HTTP status header.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- wp_doing_ajax()Determines whether the current request is a WordPress Ajax request.
- wp_die()Kills WordPress execution and displays HTML page with an error message.
Used by · 4
- wp_ajax_heartbeat()Handles the Heartbeat API via AJAX.
- wp_ajax_nopriv_heartbeat()Handles the Heartbeat API in the no-privilege context via AJAX .
- wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
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.
Signature, return type and hooks compared across 5 parsed releases.
7.1.0
Return type changed from
none to never.verified against source5.6.0
The
$flags parameter was added.from the docblock4.7.0
The
$status_code parameter was added.from the docblock3.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.