wp_send_json() – Send a JSON response back to an Ajax request.

You appear to be a bot. Output may be restricted

Description

Send a JSON response back to an Ajax request.

Usage

wp_send_json( $response, $status_code, $options );

Parameters

$response
( mixed ) required – Variable (usually an array or object) to encode as JSON, then print and die.
$status_code
( int ) optional – Optional. The HTTP status code to output. Default null.
$options
( int ) optional – Optional. Options to be passed to json_encode(). Default 0.

Returns

void

Source

File name: wordpress/wp-includes/functions.php
Lines:

1 to 35 of 35
function wp_send_json( $response, $status_code = null, $options = 0 ) {
  if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
    _doing_it_wrong(
      wp_send_json,
      sprintf(
        /* translators: 1: WP_REST_Response, 2: WP_Error */
        __( 'Return a %1$s or %2$s object from your callback when using the REST API.' ),
        'WP_REST_Response',
        'WP_Error'
      ),
      '5.5.0'
    );
  }

  if ( ! headers_sent() ) {
    header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
    if ( null !== $status_code ) {
      status_header( $status_code );
    }
  }

  echo wp_json_encode( $response, $options );

  if ( wp_doing_ajax() ) {
    wp_die(
      '',
      '',
      array(
        'response' => null,
      )
    );
  } else {
    die;
  }
}
 

 View on GitHub View on Trac