Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#35237 closed defect (bug) (fixed)

Invalid argument supplied for foreach() in /wp-includes/embed-template.php on line 54

Reported by: MarkRH Owned by: swissspidy
Priority: normal Milestone: 4.4.1
Component: Embeds Version: 4.4
Severity: normal Keywords: has-patch fixed-major
Cc: Focuses:

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)

35237.diff​ (594 bytes ) - added by swissspidy 11 years ago.
35237.2.diff​ (572 bytes ) - added by swissspidy 11 years ago.
New patch using if ( ! empty( $meta['sizes'] ) )

Download all attachments as: .zip

Change History (12)

#1 @swissspidy
11 years ago

  • Keywords has-patch added
  • Milestone Awaiting Review → 4.4.1

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.

#2 follow-up: @dd32
11 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 @swissspidy
11 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.

This ticket was mentioned in ​Slack in #core by jorbin. ​View the logs.


11 years ago

#5 @jorbin
11 years ago

  • Owner set to swissspidy
  • Status new → assigned

@swissspidy
11 years ago

New patch using if ( ! empty( $meta['sizes'] ) )

#6 @swissspidy
11 years ago

  • Resolution → fixed
  • Status assigned → closed

In 36123:

Embeds: Change attachment metadata condition to prevent a warning in the embeds template.

Fixes #35237.

#7 @swissspidy
11 years ago

  • Keywords fixed-major added
  • Resolution fixed
  • Status closed → reopened

#8 @dd32
11 years ago

  • Resolution → fixed
  • Status reopened → closed

In 36147:

Embeds: Change attachment metadata condition to prevent a warning in the embeds template.

Merges [36123] to the 4.4 branch.
Fixes #35237.

#9 @swissspidy
11 years ago

In 37452:

Embeds: Change attachment metadata condition to prevent a warning in the embeds template.

See #35237.
Fixes #36838 for trunk.

#10 @ocean90
11 years ago

In 37455:

Embeds: Change attachment metadata condition to prevent a warning in the embeds template.

Merge of [37452] to the 4.5 branch.

Props swissspidy.
See #35237.
Fixes #36838.

Note: See TracTickets for help on using tickets.