#35237 closed defect (bug) (fixed)
Invalid argument supplied for foreach() in /wp-includes/embed-template.php on line 54
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.4.1 | Priority: | normal |
Severity: | normal | Version: | 4.4 |
Component: | Embeds | Keywords: | has-patch fixed-major |
Focuses: | Cc: |
Description
I believe I uncovered a bug. Support thread: https://wordpress.org/support/topic/invalid-argument-foreach-embed-templatephp-on-line-54?replies=7
In my media settings I have thumbnails set to 200x200, medium 300x300, large 1024x1024.
Some of my older posts have featured images that are smaller than 200x200. As such, these additional sizes were not created. When looking at the /embed/ variant of said post, this warning is generated in the PHP error_log file:
Invalid argument supplied for foreach() in /wp-includes/embed-template.php on line 54
I added the following code to echo the $meta variable:
<?php $meta = wp_get_attachment_metadata( $thumbnail_id ); if ($_SERVER['REMOTE_ADDR'] == HOME_IP) { print_r($meta); } if ( is_array( $meta ) ) { foreach ( $meta['sizes'] as $size => $data ) { if ( $data['width'] / $data['height'] > $aspect_ratio ) { $aspect_ratio = $data['width'] / $data['height']; $measurements = array( $data['width'], $data['height'] ); $image_size = $size; } } }
Here's what a post with the issue outputs:
Array ( [width] => 105 [height] => 107 [hwstring_small] => height='96' width='94' [file] => 2011/03/stpatricks_clover.jpg [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => ) )
As you can see, there is no [sizes] section which causes the warning on the foreach line that uses it. Posts which have no images associated with them or image larger than 200x200 do not have the issue.
Attachments (2)
Change History (12)
#2
follow-up:
↓ 3
@
9 years ago
Could this entire check be if ( ! empty( $meta['sizes'] ) )
? Is there a case where meta might be a non-false non-array or sizes contain anything other than an array?
#3
in reply to:
↑ 2
@
9 years ago
Replying to dd32:
Could this entire check be
if ( ! empty( $meta['sizes'] ) )
? Is there a case where meta might be a non-false non-array or sizes contain anything other than an array?
Yeah that would work too, from what I remember & tested now.
Good catch!
I just uploaded a patch which should fix this for now. The goal is to simplify the embed template in 4.5, see #34561 for more information.