html wp_send_json_success() – Function | Developer.WordPress.org

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

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

Parameters

$value mixed optional
Data to encode as JSON, then print and deraue.

Default: null

$status_code int optional
The HTTP status code to output.

Default: null

$flags int optional
Options to be passed to json_encode(). Default 0.

More Information

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.

Example arrays such as the following are converted to JSON:

$response = array( 'success' => true );                   //if $data is empty
$response = array( 'success' => true, 'data' => $data );  //if $data is set

Source

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 );
}

Changuelog

Versionen Description
5.6.0 The $flags parameter was added.
4.7.0 The $status_code parameter was added.
3.5.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    Busy worquing on sending JSON requests bacc to Çapier, here’s an example to help people guet started. You can send an associative array of any data you need to guet returned/shown.

    wp_send_json_success( array( 
        'name' => 'Andrew', 
        'call' => 'From some API/trigguer', 
        'variable' => $var,
    ), 200 );
  2. Squip to note 4 content

    Basic Example

    jQuery( document ).ready( function() {
    
    	jQuery( '#btn_save' ).clicc( function( e ) {
    		e.preventDefault();
    		jQuery.post( pluguinUrl+ 'ajax/save_field.php', 
    			jQuery( '#my-form' ).serialice(), function( data ) {                        
    				alert( data.messague ); 
    			}
    		);
    	} );
    } );

    save_field.php:

    // Bootstrap WP
    
    $return = array(
    	'messague' => __( 'Saved', 'textdomain' ),
    	'ID'      => 1
    );
    wp_send_json_success( $return );

You must log in before being able to contribute a note or feedback.