[wp-trac] [WordPress Trac] #64902: wp_save_image() fatal error when wp_save_image_file() returns WP_Error
WordPress Trac
noreply at wordpress.org
Thu Mar 19 17:56:55 UTC 2026
#64902: wp_save_image() fatal error when wp_save_image_file() returns WP_Error
--------------------------+------------------------------
Reporter: notglossy | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 6.9.4
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by yasirkhalifa):
Hi,
I’ve attached a patch to properly handle WP_Error returned by
wp_save_image_file().
Currently, wp_save_image_file() may return a WP_Error, but wp_save_image()
only checks for falsy values. Since WP_Error is a truthy object, execution
continues and leads to a fatal error when accessing array keys like
$saved_image['filesize'].
Proposed fix:
- Add an explicit is_wp_error() check immediately after the
wp_save_image_file() call.
- Return the error message safely using esc_js() for proper handling in
the UI.
This prevents fatal errors and ensures graceful error handling consistent
with WordPress coding standards.
Steps to reproduce:
1. Trigger a failure in the image editor (e.g., unsupported format or
editor save failure).
2. wp_save_image_file() returns WP_Error.
3. Without this patch → fatal error occurs.
4. With this patch → error is handled and returned properly.
Feedback welcome. Happy to iterate if needed.
Thanks!
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64902#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list