WordPress.org

Make WordPress Core

Changeset 38422


Ignore:
Timestamp:
08/28/16 17:41:25 (11 months ago)
Author:
johnbillion
Message:

Formatting: Add a parameter to wp_send_json_error(), wp_send_json_success(), and wp_send_json() for specifying the HTTP response code.

Defaults to 200 in all cases, but can be used, for example, to return a 403 when using wp_send_json_error().

Fixes #35666
Props stephenharris

File:
1 edited

Legend:

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

    r38421 r38422  
    30913091 * 
    30923092 * @since 3.5.0 
    3093  * 
    3094  * @param mixed $response Variable (usually an array or object) to encode as JSON, 
    3095  *                        then print and die. 
    3096  */ 
    3097 function wp_send_json( $response ) { 
     3093 * @since 4.7.0 The `$status_code` parameter was added. 
     3094 * 
     3095 * @param mixed $response    Variable (usually an array or object) to encode as JSON, 
     3096 *                           then print and die. 
     3097 * @param int   $status_code The HTTP status code to output. 
     3098 */ 
     3099function wp_send_json( $response, $status_code = 200 ) { 
    30983100    @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); 
     3101    status_header( $status_code ); 
    30993102    echo wp_json_encode( $response ); 
    31003103    if ( wp_doing_ajax() ) 
     
    31083111 * 
    31093112 * @since 3.5.0 
    3110  * 
    3111  * @param mixed $data Data to encode as JSON, then print and die. 
    3112  */ 
    3113 function wp_send_json_success( $data = null ) { 
     3113 * @since 4.7.0 The `$status_code` parameter was added. 
     3114 * 
     3115 * @param mixed $data        Data to encode as JSON, then print and die. 
     3116 * @param int   $status_code The HTTP status code to output. 
     3117 */ 
     3118function wp_send_json_success( $data = null, $status_code = 200 ) { 
    31143119    $response = array( 'success' => true ); 
    31153120 
     
    31173122        $response['data'] = $data; 
    31183123 
    3119     wp_send_json( $response ); 
     3124    wp_send_json( $response, $status_code ); 
    31203125} 
    31213126 
     
    31303135 * @since 3.5.0 
    31313136 * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in. 
    3132  * 
    3133  * @param mixed $data Data to encode as JSON, then print and die. 
    3134  */ 
    3135 function wp_send_json_error( $data = null ) { 
     3137 * @since 4.7.0 The `$status_code` parameter was added. 
     3138 * 
     3139 * @param mixed $data        Data to encode as JSON, then print and die. 
     3140 * @param int   $status_code The HTTP status code to output. 
     3141 */ 
     3142function wp_send_json_error( $data = null, $status_code = 200 ) { 
    31363143    $response = array( 'success' => false ); 
    31373144 
     
    31513158    } 
    31523159 
    3153     wp_send_json( $response ); 
     3160    wp_send_json( $response, $status_code ); 
    31543161} 
    31553162 
Note: See TracChangeset for help on using the changeset viewer.