wppaste
WordPress

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

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

Wraps $value in a success:true JSON envelope and hands it to wp_send_json(), which prints it and immediately dies. Use it inside an Ajax callback registered on wp_ajax_{action} (and wp_ajax_nopriv_{action} for logged-out users) to signal that the request succeeded. Because it ends execution, any code placed after the call in the same callback never runs; pair it with wp_send_json_error() for the failure branch.

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.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), 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

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 post data as a successful Ajax response

An Ajax callback that looks up a post's title and its price meta, then sends them back as a successful response.

$post_id = 2;

$payload = array(
	'title' => get_the_title( $post_id ),
	'price' => get_post_meta( $post_id, 'price', true ),
);

wp_send_json_success( $payload );

echo 'this line never runs because wp_send_json_success() already called die';

wp_send_json() dies right after printing, so nothing placed after the call in a real callback ever executes; the trailing echo here is unreachable on purpose to show that.

Send a 201 status code and pretty-printed JSON on success

Some Ajax handlers need to tell the browser a resource was created and want readable JSON for debugging in the network tab.

$new_items = array( 1, 2, 3 );

wp_send_json_success(
	array( 'items' => $new_items ),
	201,
	JSON_PRETTY_PRINT
);

The $status_code and $flags arguments are only respected on WordPress versions where they were added (4.7.0 and 5.6.0 respectively); passing them on older installs has no effect.

Common problems and fixes · 4

Why is the 'data' key missing from my success response?

The source only adds $response['data'] when isset($value) is true. Calling the function with no argument, or explicitly passing null, leaves the response as just {"success":true} with no data key at all.

Why does code after wp_send_json_success() never run?

wp_send_json_success() forwards to wp_send_json(), which echoes the JSON and then calls wp_die() (or die outside an Ajax request). Execution stops at that point.

Why is my Ajax response broken JSON with extra text at the start?

wp_send_json() only sets the JSON content-type header if headers haven't been sent yet. Any PHP notice, warning, or stray whitespace/echo output before the call gets flushed first and corrupts the JSON body the browser tries to parse.

Can I set a custom HTTP status code on a success response?

Yes, the second parameter $status_code (added in 4.7.0) is passed straight through to status_header() inside wp_send_json(). It defaults to null, meaning no explicit status header is sent and the response falls back to whatever status is already in place (normally 200).

Alternatives and related functions

wp_send_json_error
When the Ajax request failed validation or processing and you need to signal success:false instead.
wp_send_json
When you need full control over the response array's shape instead of the fixed success/data envelope.
wp_die
When you need to end an Ajax request with a plain string or HTML fragment rather than JSON.
rest_ensure_response
When the endpoint is a REST API route rather than an admin-ajax.php action, since REST controllers use WP_REST_Response instead of this Ajax-specific envelope.

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.10 and 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

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