php - WordPress 5 WP REST routes - No errors

admin2025-06-04  2

I'm using WP JSON to return JSON for certain routes.

Prior to WordPress 5, WP JSON would return PHP errors in the response, e.g. Notice:xxx or Warning:xxx when a PHP error was encountered. Since I upgraded to WordPress 5, such errors are supressed/ skipped and JSON is returned. Fatal errors return a 500 as expected.

I cannot find any documentation identifying why these errors are not being returned. And Google doesn't seem to be shedding any light. Does anybody know what's going on? Also, if it's "toggleable", does anyone know how to "turn errors on"?

PHP outputs errors to screen as usual elsewhere in WordPress.

I'm using WP JSON to return JSON for certain routes.

Prior to WordPress 5, WP JSON would return PHP errors in the response, e.g. Notice:xxx or Warning:xxx when a PHP error was encountered. Since I upgraded to WordPress 5, such errors are supressed/ skipped and JSON is returned. Fatal errors return a 500 as expected.

I cannot find any documentation identifying why these errors are not being returned. And Google doesn't seem to be shedding any light. Does anybody know what's going on? Also, if it's "toggleable", does anyone know how to "turn errors on"?

PHP outputs errors to screen as usual elsewhere in WordPress.

Share Improve this question asked Jan 20, 2019 at 1:48 dewddewd 17911 bronze badges 5
  • Do they not appear in your servers PHP error log file? Keep in mind that warnings would mean invalid json gets output which would break many sites – Tom J Nowell Commented Jan 20, 2019 at 3:15
  • You probably had WP_DEBUG enabled, hence you got those PHP notices and warnings? So you could enable it back and see if the notices/warnings come up. But about why it was suddenly disabled, I don't know for sure. – Sally CJ Commented Jan 20, 2019 at 6:20
  • @TomJNowell yes they appear in the PHP log. Fatal errors produce invalid JSON. Whilst in development, it would be very useful if Notices, and Warnings did too. – dewd Commented Jan 20, 2019 at 19:54
  • @SallyCJ WP_DEBUG is enabled and errors show for WordPress generally, just not via calls to the JSON API. – dewd Commented Jan 20, 2019 at 19:55
  • Rather than trying to undo the fix to show the errors in the browser, consider alternatives that give far more options, such as debuggers, error handlers, etc – Tom J Nowell Commented Jan 20, 2019 at 20:07
Add a comment  | 

1 Answer 1

Reset to default 1

REST API should always return JSON, so it’s output can be parsed as such. If it’ll return something that is not correct JSON, then your JS scripts may stop working.

This is why there should be no errors printed in these requests - such messages break JSON response and make it hard to parse.

And there was some code that was trying to prevent such messages from occurring, but it wasn’t working properly.

This was fixed in 5.0 in bug #44534.

You can always add this code to your method, id you're really (really really really) sure, what you're doing:

if ( true === WP_DEBUG ) @ini_set( 'display_errors', 1 );

PS. You should never display errors on production server - it’s compromising security of your site. Always use files as logging method. (It’s also easier)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748991917a315440.html

最新回复(0)