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:4560
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.
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?
- Why does code after wp_send_json_success() never run?
- Why is my Ajax response broken JSON with extra text at the start?
- Can I set a custom HTTP status code on a success response?
Why is the 'data' key missing from my success response?
Why does code after wp_send_json_success() never run?
Why is my Ajax response broken JSON with extra text at the start?
Can I set a custom HTTP status code on a success response?
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
- Scaling
- Constant
- Instructions
- 12–14
- Plugin surface
- None
- Called by
- 50
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 14.
Nothing here hands control to plugin code.
50 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()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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–14 | wp_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
- wp_send_json()Sends a JSON response back to an Ajax request.
Used by · 50
- Custom_Background::ajax_background_add()Handles Ajax request for adding custom background context to an attachment.
- Custom_Image_Header::ajax_header_add()Given an attachment ID for a header image, updates its "last used" timestamp to now.
- Custom_Image_Header::ajax_header_crop()Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.
- Custom_Image_Header::ajax_header_remove()Given an attachment ID for a header image, unsets it as a user-uploaded header image for the active theme.
- WP_Customize_Manager::handle_changeset_trash_request()Handles request to trash a changeset.
- WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock.
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
- WP_Customize_Manager::handle_override_changeset_lock_request()Removes changeset lock when take over request is sent via Ajax.
- WP_Customize_Manager::refresh_nonces()Refreshes nonces for the current preview.
- WP_Customize_Manager::save()Handles customize_save WP Ajax request to save/update a changeset.
- WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()Ajax handler for adding a new auto-draft post.
- WP_Customize_Nav_Menus::ajax_load_available_items()Ajax handler for loading available menu items.
Show all 50
- WP_Customize_Nav_Menus::ajax_search_available_items()Ajax handler for searching available menu items.
- WP_Customize_Selective_Refresh::handle_render_partials_request()Handles the Ajax request to return the rendered partials for the requested placements.
- WP_Customize_Widgets::wp_ajax_update_widget()Updates widget settings asynchronously.
- WP_Plugin_Dependencies::check_plugin_dependencies_during_ajax()Checks plugin dependencies after a plugin is installed via AJAX.
- locate_block_template()Finds a block template with equal or higher specificity than a given PHP template file.
- wp_ajax_activate_plugin()Handles activating a plugin via AJAX.
- wp_ajax_crop_image()Handles cropping an image via AJAX.
- wp_ajax_delete_plugin()Handles deleting a plugin via AJAX.
- wp_ajax_delete_theme()Handles deleting a theme via AJAX.
- wp_ajax_destroy_sessions()Handles destroying multiple open sessions for a user via AJAX.
- wp_ajax_edit_theme_plugin_file()Handles editing a theme or plugin file via AJAX.
- wp_ajax_find_posts()Handles querying posts for the Find Posts modal via AJAX.
- wp_ajax_generate_password()Handles generating a password via AJAX.
- wp_ajax_get_attachment()Handles getting an attachment via AJAX.
- wp_ajax_get_community_events()Handles Ajax requests for community events
- wp_ajax_get_post_thumbnail_html()Handles retrieving HTML for the featured image via AJAX.
- wp_ajax_get_revision_diffs()Handles getting revision diffs via AJAX.
- wp_ajax_health_check_background_updates()Handles site health checks on background updates via AJAX.
- wp_ajax_health_check_dotorg_communication()Handles site health checks on server communication via AJAX.
- wp_ajax_health_check_get_sizes()Handles site health check to get directories and database sizes via AJAX.
- wp_ajax_health_check_loopback_requests()Handles site health checks on loopback requests via AJAX.
- wp_ajax_health_check_site_status_result()Handles site health check to update the result status via AJAX.
- wp_ajax_image_editor()Handles image editing via AJAX.
- wp_ajax_install_plugin()Handles installing a plugin via AJAX.
- wp_ajax_install_theme()Handles installing a theme via AJAX.
- wp_ajax_media_create_image_subsizes()Handles creating missing image sub-sizes for just uploaded images via AJAX.
- wp_ajax_meta_box_order()Handles saving the meta box order via AJAX.
- wp_ajax_nopriv_generate_password()Handles generating a password in the no-privilege context via AJAX.
- wp_ajax_parse_embed()Applies [embed] Ajax handlers to a string.
- wp_ajax_parse_media_shortcode()
- wp_ajax_query_attachments()Handles querying attachments via AJAX.
- wp_ajax_query_themes()Handles getting themes from themes_api() via AJAX.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
- wp_ajax_save_attachment_compat()Handles saving backward compatible attachment attributes via AJAX.
- wp_ajax_save_attachment_order()Handles saving the attachment order via AJAX.
- wp_ajax_save_user_color_scheme()Handles auto-saving the selected color scheme for a user's own profile via AJAX.
- wp_ajax_save_wporg_username()Handles saving the user's WordPress.org username via AJAX.
- wp_ajax_search_install_plugins()Handles searching plugins to install via AJAX.
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.
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 6.7.10 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.