[wp-trac] [WordPress Trac] #64901: rest_convert_error_to_response() fatals when WP_Error data is a stdClass object in 7.0-beta5
WordPress Trac
noreply at wordpress.org
Thu Mar 19 14:09:19 UTC 2026
#64901: rest_convert_error_to_response() fatals when WP_Error data is a stdClass
object in 7.0-beta5
--------------------------+-----------------------------
Reporter: nateallen | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version:
Severity: critical | Keywords:
Focuses: rest-api |
--------------------------+-----------------------------
The `rest_convert_error_to_response()` function throws a PHP fatal error
when a `WP_Error` object contains error data that is a `stdClass` instead
of an array.
{{{
PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as
array
in wp-includes/rest-api.php on line 3431
}}}
== Cause ==
In [changeset:61429] (part of the Code Modernization effort, #63430), the
`isset()` ternary in `rest_convert_error_to_response()` was replaced with
a null coalescing operator:
**Before (6.9.4):**
{{{#!php
<?php
return is_array( $error_data ) && isset( $error_data['status'] ) ?
$error_data['status'] : $status;
}}}
**After (7.0-beta5):**
{{{#!php
<?php
return $error_data['status'] ?? $status;
}}}
The `is_array()` guard was critical here because
`WP_Error::get_all_error_data()` can return data of any type, such as
arrays, objects, strings, integers, etc. Removing it causes a fatal when
error data is a `stdClass`.
== Steps to reproduce ==
Register a REST route that returns a `WP_Error` with `stdClass` data:
{{{#!php
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'test/v1', '/trigger-bug', array(
'methods' => 'GET',
'callback' => function () {
return new WP_Error(
'test_error',
'Test error',
(object) array( 'status' => 500 )
);
},
'permission_callback' => '__return_true',
) );
} );
}}}
Then request `GET /wp-json/test/v1/trigger-bug`.
== Expected result ==
A JSON error response (as in 6.9.4).
== Actual result ==
PHP fatal error on line 3431 of `wp-includes/rest-api.php`.
== Suggested fix ==
Restore the `is_array()` check:
{{{#!php
<?php
return is_array( $error_data ) ? ( $error_data['status'] ?? $status ) :
$status;
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64901>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list