wp_send_json

The timeline below displays how wordpress function wp_send_json has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Sends a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 * @since 5.6.0 The `$flags` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code Optional. The HTTP status code to output. Default null.
 * @param int   $flags       Optional. Options to be passed to json_encode(). Default 0.
 */
function wp_send_json($response, $status_code = null, $flags = 0)
{
    if (wp_is_serving_rest_request()) {
        _doing_it_wrong(__FUNCTION__, 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, $flags);
    if (wp_doing_ajax()) {
        wp_die('', '', array('response' => null));
    } else {
        die;
    }
}

WordPress Version: 6.1

/**
 * Sends a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 * @since 5.6.0 The `$options` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code Optional. The HTTP status code to output. Default null.
 * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
 */
function wp_send_json($response, $status_code = null, $options = 0)
{
    if (defined('REST_REQUEST') && REST_REQUEST) {
        _doing_it_wrong(__FUNCTION__, 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;
    }
}

WordPress Version: 5.6

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 * @since 5.6.0 The `$options` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code Optional. The HTTP status code to output. Default null.
 * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
 */
function wp_send_json($response, $status_code = null, $options = 0)
{
    if (defined('REST_REQUEST') && REST_REQUEST) {
        _doing_it_wrong(__FUNCTION__, 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;
    }
}

WordPress Version: 5.5

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code The HTTP status code to output.
 */
function wp_send_json($response, $status_code = null)
{
    if (defined('REST_REQUEST') && REST_REQUEST) {
        _doing_it_wrong(__FUNCTION__, 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);
    if (wp_doing_ajax()) {
        wp_die('', '', array('response' => null));
    } else {
        die;
    }
}

WordPress Version: 5.3

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code The HTTP status code to output.
 */
function wp_send_json($response, $status_code = null)
{
    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);
    if (wp_doing_ajax()) {
        wp_die('', '', array('response' => null));
    } else {
        die;
    }
}

WordPress Version: 4.7

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 * @since 4.7.0 The `$status_code` parameter was added.
 *
 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
 *                           then print and die.
 * @param int   $status_code The HTTP status code to output.
 */
function wp_send_json($response, $status_code = null)
{
    @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
    if (null !== $status_code) {
        status_header($status_code);
    }
    echo wp_json_encode($response);
    if (wp_doing_ajax()) {
        wp_die('', '', array('response' => null));
    } else {
        die;
    }
}

WordPress Version: 4.1

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 *
 * @param mixed $response Variable (usually an array or object) to encode as JSON,
 *                        then print and die.
 */
function wp_send_json($response)
{
    @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
    echo wp_json_encode($response);
    if (defined('DOING_AJAX') && DOING_AJAX) {
        wp_die();
    } else {
        die;
    }
}

WordPress Version: 4.0

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 *
 * @param mixed $response Variable (usually an array or object) to encode as JSON,
 *                        then print and die.
 */
function wp_send_json($response)
{
    @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
    echo json_encode($response);
    if (defined('DOING_AJAX') && DOING_AJAX) {
        wp_die();
    } else {
        die;
    }
}

WordPress Version: 3.7

/**
 * Send a JSON response back to an Ajax request.
 *
 * @since 3.5.0
 *
 * @param mixed $response Variable (usually an array or object) to encode as JSON, then print and die.
 */
function wp_send_json($response)
{
    @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
    echo json_encode($response);
    if (defined('DOING_AJAX') && DOING_AJAX) {
        wp_die();
    } else {
        die;
    }
}