Opened 4 years ago
Last modified 8 days ago
#54327 new feature request
Support wp_die from array print
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.8.1 |
Component: | General | Keywords: | has-patch changes-requested |
Focuses: | Cc: |
Description
I think wp_die() function is very used in wordpress plugin and theme developement and that is better to support array when pass an array to this and prevent from array to string notice.
in wp-includes/functions.php
line ~3800
before:
echo $message;
after:
if(is_array($message)) { print_r($message); }else { echo $message; }
tanx.
Attachments (2)
Change History (8)
#2
@
4 years ago
Hi there, welcome to WordPress Trac! Thanks for the ticket.
I think the patch might have to be a bit more complex than that, to support all the current wp_die()
handlers in core:
_default_wp_die_handler()
_ajax_wp_die_handler()
_json_wp_die_handler()
_jsonp_wp_die_handler()
_xmlrpc_wp_die_handler()
_xml_wp_die_handler()
_scalar_wp_die_handler()
I also think we'd want to avoid using print_r()
as its output is a bit too technical, and instead update _wp_die_process_input()
to use the first item of the array as the main message, and add the rest to $args['additional_errors']
, like we already do if $message
is a WP_Error
object, see lines 4076 and 4088.
#3
@
4 years ago
tnx @SergeyBiryukov
Of course, I mean using this method for the developers in plugin or theme development. Because of this, the technicality of the output is not a problem. Rather, my goal in printing arrays or objects is to see their content very quickly. (first element does not help)
Otherwise, I know that from the user's point of view, and for items that are used in printing errors, such as wp_error object, it may create restrictions.
tanx again.
#4
@
4 years ago
@SergeyBiryukov I took a look into _wp_die_process_input()
. I think there is nothing to modify to achieve the proposed result. that mentioned method nicely handling the $message
returning as an array. In order to display the user input array, we must print it in some way. I think print_r()
is a decent way to print the array. _wp_die_process_input()
already checking if it's an error object coming from WordPress itself or not and the $message
is always a string unless the user inputs an array as $message.
I agree with you regarding modifying all error handlers.
_default_wp_die_handler()
: Current patch should work fine. But output can be nicer with<pre></pre>
_ajax_wp_die_handler()
: Same as_default_wp_die_handler
but without<pre></pre>
_json_wp_die_handler()
: Current code should work just fine_jsonp_wp_die_handler()
: Current code should work just fine_xmlrpc_wp_die_handler()
: As error output is always string forIXR_Error
we canjson_encode
the $message if is an array._xml_wp_die_handler()
: same solution as_xmlrpc_wp_die_handler
_scalar_wp_die_handler()
: I couldn't find any use of this handler. I think it's possible to skip this or remove this.
I'll be adding a patch with my update across all handlers.
#5
@
8 days ago
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
- ✅
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
- ❌
_ajax_wp_die_handler()
still outputs only0
when passed an array.
Additional Notes
_ajax_wp_die_handler()
overrides any array passed towp_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.
_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
_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
_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 thewp_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
_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
Sounds like a valid proposal to me. Adding a patch.