WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp send json success

Description

Send a JSON response back to an Ajax request, indicating success. The response object will always have a success key with the value true. If anything is passed to the function it will be encoded as the value for a data key.

Usage

<?php wp_send_json_success$data ); ?> Such array is converted to json: $response = array( 'success' => true );                   //if $data is empty
$response = array( 'success' => true, 'data' => $data );  //if $data is set 

Parameters

$data
(mixed) (optional) Data to encode as JSON, then print and die.
Default: null

Examples

jQuery(document).ready(function(){
    jQuery('#btn_save').click(function(e) {
        e.preventDefault();
        jQuery.post(
            pluginUrl + 'ajax/save_field.php',
            jQuery('#my-form').serialize(),
            function( data ) {
                if ( data.success ) {
                    alert( data.message );
                }
            }
        );
    });
});

save_field.php

<?php
if ( verify_nonce_field(...) ) {
    wp_send_json_error();
} else {
    $response = array(
        'message' => 'Saved',
        'ID'      => 1,
    );
    wp_send_json_success( $response );
}

Notes

Uses wp_send_json() to send response.

Change Log

Source File

wp_send_json_success() is located in wp-includes/functions.php

Related

wp_send_json(), wp_send_json_error()