WordPress.org

Make WordPress Core

Changeset 49235


Ignore:
Timestamp:
10/20/2020 03:54:43 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

General: Add $options parameter to JSON response functions:

  • wp_send_json()
  • wp_send_json_success()
  • wp_send_json_error()

This allows for customizing the options passed to json_encode().

Props eroraghav, hareesh-pillai, garrett-eclipse.
Fixes #51293.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r49225 r49235  
    40794079 * @since 3.5.0
    40804080 * @since 4.7.0 The `$status_code` parameter was added.
     4081 * @since 5.6.0 The `$options` parameter was added.
    40814082 *
    40824083 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
    40834084 *                           then print and die.
    4084  * @param int   $status_code The HTTP status code to output.
    4085  */
    4086 function wp_send_json( $response, $status_code = null ) {
     4085 * @param int   $status_code Optional. The HTTP status code to output. Default null.
     4086 * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
     4087 */
     4088function wp_send_json( $response, $status_code = null, $options = 0 ) {
    40874089    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
    40884090        _doing_it_wrong(
     
    41054107    }
    41064108
    4107     echo wp_json_encode( $response );
     4109    echo wp_json_encode( $response, $options );
    41084110
    41094111    if ( wp_doing_ajax() ) {
     
    41254127 * @since 3.5.0
    41264128 * @since 4.7.0 The `$status_code` parameter was added.
    4127  *
    4128  * @param mixed $data        Data to encode as JSON, then print and die.
    4129  * @param int   $status_code The HTTP status code to output.
    4130  */
    4131 function wp_send_json_success( $data = null, $status_code = null ) {
     4129 * @since 5.6.0 The `$options` parameter was added.
     4130 *
     4131 * @param mixed $data        Optional. Data to encode as JSON, then print and die. Default null.
     4132 * @param int   $status_code Optional. The HTTP status code to output. Default null.
     4133 * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
     4134 */
     4135function wp_send_json_success( $data = null, $status_code = null, $options = 0 ) {
    41324136    $response = array( 'success' => true );
    41334137
     
    41364140    }
    41374141
    4138     wp_send_json( $response, $status_code );
     4142    wp_send_json( $response, $status_code, $options );
    41394143}
    41404144
     
    41504154 * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
    41514155 * @since 4.7.0 The `$status_code` parameter was added.
    4152  *
    4153  * @param mixed $data        Data to encode as JSON, then print and die.
    4154  * @param int   $status_code The HTTP status code to output.
    4155  */
    4156 function wp_send_json_error( $data = null, $status_code = null ) {
     4156 * @since 5.6.0 The `$options` parameter was added.
     4157 *
     4158 * @param mixed $data        Optional. Data to encode as JSON, then print and die. Default null.
     4159 * @param int   $status_code Optional. The HTTP status code to output. Default null.
     4160 * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
     4161 */
     4162function wp_send_json_error( $data = null, $status_code = null, $options = 0 ) {
    41574163    $response = array( 'success' => false );
    41584164
     
    41754181    }
    41764182
    4177     wp_send_json( $response, $status_code );
     4183    wp_send_json( $response, $status_code, $options );
    41784184}
    41794185
Note: See TracChangeset for help on using the changeset viewer.