wppaste
WordPress

wp_send_json_success( mixed $value = null, int $status_code = null, int $flags = 0 ): never

Since
3.5.0, 4.7.0, 5.6.0
Source
wp-includes/functions.php:4635

Wraps a value in a success flagged array and hands it to wp_send_json() for output, then halts execution with wp_die(). Since WordPress 5.6.0 it also accepts an HTTP status code and json_encode() flags, letting an Ajax handler reply with something like 201 Created instead of the default 200. Because its return type is never, any code written after the call inside the same handler will not run. Pair it with wp_send_json_error() for the failure branch of the same Ajax action.

Sends a JSON response back to an Ajax request, indicating success.

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

$valuemixedoptional
Data to encode as JSON, then print and die. Default null.Default: null
$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

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Return a post's title and meta from an Ajax handler as a JSON success response

A wp_ajax_ callback that looks up post 2 and its price meta, then replies to the Ajax request.

$post  = get_post( 2 );
$price = get_post_meta( 2, 'price', true );

wp_send_json_success(
	array(
		'title' => $post->post_title,
		'price' => $price,
	)
);

echo 'This line never runs.';

wp_send_json_success() calls wp_die() internally, so the echo after it is unreachable, the sandbox output is the JSON printed by wp_send_json() itself.

Send a JSON success response with a custom HTTP status code and json_encode() flags

An Ajax handler that creates a draft post and wants the response to carry a 201 status instead of the default 200.

$post_id = wp_insert_post(
	array(
		'post_title'  => 'Draft created via Ajax',
		'post_status' => 'draft',
		'post_type'   => 'post',
	)
);

wp_send_json_success(
	array( 'id' => $post_id ),
	201,
	JSON_UNESCAPED_SLASHES
);

The $status_code and $flags parameters only exist from WordPress 5.6.0 onward.

Common problems and fixes · 4

Why does my Ajax response come back as HTML wrapped around the JSON instead of clean JSON?

wp_send_json_success() ends by calling wp_die() through wp_send_json(). If the request isn't recognized as a real Ajax or REST request, wp_die() falls back to its default HTML death screen after the JSON has already been echoed.

Why is response.data missing on the client instead of being null?

The source only sets $response['data'] = $value when isset($value) is true. Calling wp_send_json_success() with no argument (its default is null) produces a response of just {"success":true}, with no data key at all.

Why does code I put after wp_send_json_success() never execute?

The function's return type is never. It hands off to wp_send_json(), which calls wp_die() and terminates the request, so any statements written after the call in the same handler are dead code.

Why does passing a status code do nothing on my site?

The $status_code and $flags parameters were added in WordPress 5.6.0 (the function's since array lists 3.5.0, 4.7.0, 5.6.0). On an older core, or if a plugin overrides the function, only $value is honored and the status is ignored.

Alternatives and related functions

wp_send_json_error
When the Ajax request failed and the response should carry success: false along with error details.
wp_send_json
When the response needs a shape other than {success, data}, since wp_send_json_success is just a thin wrapper around it.
rest_ensure_response
When building a REST API endpoint rather than a classic admin-ajax.php handler.
wp_die
When more control over the die handler or headers is needed than wp_send_json provides.

Performance profile

How much work a call to wp_send_json_success() 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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–14

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 14.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()one call below wp_send_json_success()

Further down the call graph this can also reach hook, cache, serialize, 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 · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_send_json_success() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always12–14wp_send_json()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 14 instructions, 12–14 executed per call, 1 branch. The work does not change between versions.

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 · 1

Used by · 50

Show all 50

Source code

function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {	$response = array( 'success' => true ); 	if ( isset( $value ) ) {		$response['data'] = $value;	} 	wp_send_json( $response, $status_code, $flags );}

Changelog

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.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.