Opened 12 months ago
Last modified 3 months ago
#60480 assigned defect (bug)
Warning: Undefined array key "file" in .../wp-includes/media.php on line 1723
Reported by: | shanemac10 | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | major | Version: | 6.4.3 |
Component: | Import | Keywords: | has-patch |
Focuses: | Cc: |
Description
When running Tools > Import > Run Importer, then selecting "Download and import file attachments" on the import options, the media (what appears to be a native Featured Image in my case) does not import, and displays this following message for each failed media import:
Warning: Undefined array key "file" in .../wp-includes/media.php on line 1723
When looking into the matter, it appears in wp_img_tag_add_srcset_and_sizes_attr on line 2154 and 2155 where wp_get_attachment_metadata is called, which may return false, then it's sent directly into wp_image_add_srcset_and_sizes, where it (on 1723) tests a preg_match on it without testing if $image_meta is an array or if key_exists, etc.
2154 $image_meta = wp_get_attachment_metadata( $attachment_id ); // <--- returns array|false 2155 return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
1723 if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) // <--- $image_meta may be false
Get blame says this code has been around for over 4 years.
https://github.com/WordPress/wordpress-develop/blame/6.4/src/wp-includes/media.php#L2138-L2159
My scenario is importing a custom post type (that is identical to generic post type) with only WYSIWYG content and a featured image, to a local dev site on my personal computer from a public site (w/ full DNS with SSL cert), so there should not be an issue with fetching the images I believe. Either way, this error is a bug, and needs a way to fail more gracefully if the issue is else where in the Import process.
Change History (3)
#2
@
4 months ago
I had similar errors logged, and I traced it down to when sites were using Images that were generated by uploaded PDFs. The metadata for those pdf thumbnails does not seem to contain the base 'file' attribute.
What I can't actually figure out is how our users even got the URLs to use those images on the front-end! 😅 .
This ticket was mentioned in PR #7691 on WordPress/wordpress-develop by @debarghyabanerjee.
3 months ago
#3
- Keywords has-patch added
Trac Ticket: Core-60480
## Description
- This pull request addresses a warning encountered during the media import process when using the "Download and import file attachments" option in the Tools > Import section of WordPress. A warning is triggered due to an attempt to access an undefined array key in the
wp_image_add_srcset_and_sizes
function.
## Proposed Changes
- Added an
isset()
check before accessing the 'file' key of the$image_meta
array to prevent the warning from occurring.
- This adjustment ensures that the code handles cases where media metadata retrieval fails more gracefully, avoiding the warning message.
- Additionally, this change is aimed at bringing consistency across all functions that interact with attachment metadata, ensuring that similar checks are uniformly applied throughout the codebase.
As an additional note: I made the following change to the code, and re-ran the import. The images did not import, and there were no errors printed either.
Added...
Like so...