Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#35237 closed defect (bug) (fixed)

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

Reported by: markrh's profile MarkRH Owned by: swissspidy's profile swissspidy
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)

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

Download all attachments as: .zip

Change History (12)

@swissspidy
9 years ago

#1 @swissspidy
9 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 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
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 @swissspidy
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.

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


9 years ago

#5 @jorbin
9 years ago

  • Owner set to swissspidy
  • Status changed from new to assigned

@swissspidy
9 years ago

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

#6 @swissspidy
9 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 36123:

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

Fixes #35237.

#7 @swissspidy
9 years ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

#8 @dd32
9 years ago

  • Resolution set to fixed
  • Status changed from reopened to 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
9 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
9 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.