[wp-trac] [WordPress Trac] #54327: Support wp_die from array print

WordPress Trac noreply at wordpress.org
Mon Jun 30 16:42:17 UTC 2025


#54327: Support wp_die from array print
-------------------------------------+------------------------------
 Reporter:  myousefi08               |       Owner:  (none)
     Type:  feature request          |      Status:  new
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  General                  |     Version:  5.8.1
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+------------------------------

Comment (by devasheeshkaul):

 == Test Report
 === Description
 This report validates whether the indicated patch works as expected.

 === Environment
 - WordPress: 6.9-alpha-60093-src
 - PHP: 8.2.28
 - Server: nginx/1.29.0
 - Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
 - Browser: Chrome 137.0.0.0
 - OS: macOS
 - Theme: Twenty Twenty-Five 1.2
 - MU Plugins: None activated
 - Plugins:
   * Test Reports 1.2.0

 === Actual Results
 1. ✅ `wp_die()` with an array now outputs structured results for the
 following handlers:
    - `_default_wp_die_handler()` – array is printed as expected
    - `_json_wp_die_handler()`/`_jsonp_wp_die_handler()` – array is
 returned as JSON
    - `_xml_wp_die_handler()`/`_xmlrpc_wp_die_handler()` – array is
 serialized properly in XML

 2. ❌ `_ajax_wp_die_handler()` still outputs only `0` when passed an
 array.

 === Additional Notes

 - `_ajax_wp_die_handler()` overrides any array passed to `wp_die()` by
 converting it directly to `"0"` unless it is scalar. Check this:
 [https://github.com/WordPress/wordpress-
 develop/blob/a27baba9f4aa58b467caf47154ed3e1eb5c2252d/src/wp-
 includes/functions.php#L4065-L4069]

 - This occurs before any opportunity for the patch to render the array
 using `print_r()`.

 - To improve `_ajax_wp_die_handler()` while maintaining backward
 compatibility, one option is to add an additional check for when
 `$message` is an array or object, and output it as formatted text or JSON:
 {{{
 if ( is_array( $message ) || is_object( $message ) ) {
     $message = print_r( $message, true ); // Or wp_json_encode( $message )
 } elseif ( ! is_scalar( $message ) ) {
     $message = '0';
 } else {
     $message = (string) $message;
 }
 }}}


 === Supplemental Artifacts
 Below are the code examples used to verify the behavior of `wp_die()` when
 passed an array, across various die handlers. You can add these snippets
 to your plugin's main file or the active theme’s `functions.php` to test
 locally.

 1. `_default_wp_die_handler()`

 {{{
 add_action( 'init', function () {
         if ( isset( $_GET['test-default-die'] ) ) {
                 wp_die( array( 'error' => 'Something went wrong', 'code'
 => 500 ) );
         }
 } );
 }}}
 Visit `/wp-admin/?test-default-die` to check output

 2. `_json_wp_die_handler()` (same internal logic for
 `_jsonp_wp_die_handler()`)

 {{{
 add_action( 'rest_api_init', function () {
         register_rest_route( 'test/v1', '/wp-die-json', [
                 'methods'  => 'GET',
                 'callback' => 'test_wp_die_json_handler',
                 'permission_callback' => '__return_true',
         ] );
 } );

 function test_wp_die_json_handler() {
         wp_die( array( 'json_error' => 'Something failed', 'code' => 500 )
 );
 }
 }}}
 Visit `/wp-json/test/v1/wp-die-json` to check output

 3. `_xml_wp_die_handler()` (same patch logic used for
 `_xmlrpc_wp_die_handler()`). This test manually forces the use of
 `_xml_wp_die_handler()` via the `wp_die_handler` filter.


 {{{
 add_action( 'init', function () {
         if ( isset( $_GET['test-xml-die'] ) ) {
                 add_filter( 'wp_die_handler', function () {
                         return '_xml_wp_die_handler';
                 } );
                 wp_die( array( 'xml' => 'Something broke', 'code' => 999 )
 );
         }
 } );
 }}}
 Visit `/?test-xml-die` to check output

 4. `_ajax_wp_die_handler()`

 {{{
 add_action( 'wp_ajax_test_ajax_die', function () {
     wp_die( array( 'ajax' => 'AJAX failed', 'code' => 403 ) );
 } );
 }}}
 Visit `/wp-admin/admin-ajax.php?action=test_ajax_die` to check output

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54327#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list