wp_send_json( mixed $response, int $status_code = null, int $flags = 0 ): never
- Since
- 3.5.0, 4.7.0, 5.6.0
- Source
wp-includes/functions.php:4587
Compatibility
- WordPress
- since 5.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
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
Return value
never
Performance profile
How much work a call to wp_send_json() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 20–48
- Plugin surface
- None
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 51.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()one call below wp_send_json() - cacheobject cache
wp_cache_get()one call below wp_send_json() - serializeserialisation
maybe_unserialize()one call below wp_send_json()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_send_json() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!wp_is_serving_rest_request() && headers_sent() && !wp_doing_ajax() | 20 | wp_is_serving_rest_request(), headers_sent(), wp_json_encode(), wp_doing_ajax(), exit() |
!wp_is_serving_rest_request() && headers_sent() && wp_doing_ajax() | 23 | wp_is_serving_rest_request(), headers_sent(), wp_json_encode(), wp_doing_ajax(), wp_die() |
!wp_is_serving_rest_request() && !headers_sent() && $status_code === null && !wp_doing_ajax() | 29 | wp_is_serving_rest_request(), headers_sent(), get_option(), header(), wp_json_encode(), wp_doing_ajax(), exit() |
!wp_is_serving_rest_request() && !headers_sent() && $status_code === null && wp_doing_ajax() | 32 | wp_is_serving_rest_request(), headers_sent(), get_option(), header(), wp_json_encode(), wp_doing_ajax(), wp_die() |
!wp_is_serving_rest_request() && !headers_sent() && $status_code !== null && !wp_doing_ajax() | 32 | wp_is_serving_rest_request(), headers_sent(), get_option(), header(), status_header(), wp_json_encode(), wp_doing_ajax(), exit() |
wp_is_serving_rest_request() && headers_sent() && !wp_doing_ajax() | 33 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), wp_json_encode(), wp_doing_ajax(), exit() |
!wp_is_serving_rest_request() && !headers_sent() && $status_code !== null && wp_doing_ajax() | 35 | wp_is_serving_rest_request(), headers_sent(), get_option(), header(), status_header(), wp_json_encode(), wp_doing_ajax(), wp_die() |
wp_is_serving_rest_request() && headers_sent() && wp_doing_ajax() | 36 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), wp_json_encode(), wp_doing_ajax(), wp_die() |
wp_is_serving_rest_request() && !headers_sent() && $status_code === null && !wp_doing_ajax() | 42 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), get_option(), header(), wp_json_encode(), wp_doing_ajax(), exit() |
wp_is_serving_rest_request() && !headers_sent() && $status_code === null && wp_doing_ajax() | 45 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), get_option(), header(), wp_json_encode(), wp_doing_ajax(), wp_die() |
wp_is_serving_rest_request() && !headers_sent() && $status_code !== null && !wp_doing_ajax() | 45 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), get_option(), header(), status_header(), wp_json_encode(), wp_doing_ajax(), exit() |
wp_is_serving_rest_request() && !headers_sent() && $status_code !== null && wp_doing_ajax() | 48 | wp_is_serving_rest_request(), __(), sprintf(), _doing_it_wrong(), headers_sent(), get_option(), header(), status_header(), wp_json_encode(), wp_doing_ajax(), wp_die() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 51 | 20–48 | 4 | |
| 8.5 | 51 | 20–48 | 4 | |
| 8.4 | 51 | 20–48 | 4 | 2 more instructions than PHP 8.3 |
| 8.3 | 49 | 18–48 | 4 | |
| 8.2 | 49 | 18–48 | 4 | |
| 8.1 | 49 | 18–48 | 4 | |
| 7.4 | 49 | 18–48 | 4 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
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 code
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; }}Changelog
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.
none to never.verified against source$flags parameter was added.from the docblock$status_code parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.