Make WordPress Core

Changeset 30506


Ignore:
Timestamp:
11/21/2014 04:55:28 PM (12 years ago)
Author:
johnbillion
Message:

Add support for WP_Error objects passed to wp_send_json_error(). The error object gets output as an array of error codes and messages, rather than as an empty object.

Fixes #28978
Props paulschreiber

File:
1 edited

Legend:

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

    r30379 r30506  
    28022802 * Send a JSON response back to an Ajax request, indicating failure.
    28032803 *
     2804 * If the `$data` parameter is a `WP_Error` object, the errors within the object are processed
     2805 * and output as an array of error codes and corresponding messages. All other types are
     2806 * output without further processing.
     2807 *
    28042808 * @since 3.5.0
     2809 * @since 4.1.0 The `$data` parameter is now processed if a `WP_Error` object is passed in.
    28052810 *
    28062811 * @param mixed $data Data to encode as JSON, then print and die.
     
    28092814        $response = array( 'success' => false );
    28102815
    2811         if ( isset( $data ) )
    2812                 $response['data'] = $data;
     2816        if ( isset( $data ) ) {
     2817                if ( is_wp_error( $data ) ) {
     2818                        $result = array();
     2819                        foreach ( $data->errors as $code => $messages ) {
     2820                                foreach ( $messages as $message ) {
     2821                                        $result[] = array( 'code' => $code, 'message' => $message );
     2822                                }
     2823                        }
     2824
     2825                        $response['data'] = $result;
     2826                } else {
     2827                        $response['data'] = $data;
     2828                }
     2829        }
    28132830
    28142831        wp_send_json( $response );
Note: See TracChangeset for help on using the changeset viewer.