Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#54701 closed defect (bug) (duplicate)

Twenty Twenty-One: twenty_twenty_one_get_attachment_image_attributes() missing wp_get_attachment_metadata() check

Reported by: justlevine's profile justlevine Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.8.2
Component: Bundled Theme Keywords:
Focuses: Cc:

Description

Im getting a PHP notice from inc\template-functions.php::twenty_twenty_one_get_attachment_image_attributes() about accessing an array value on a boolean.

Issue stems from:

<?php
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( $meta['width'] && $meta['height'] ) {
  $width  = (int) $meta['width'];
  $height = (int) $meta['height'];
}

since wp_get_attachment_meta() can return false when no attachment id exists.

Trac stresses me out, but here's the fix:

<?php
$meta = wp_get_attachment_metadata( $attachment->ID );
if( false === $meta ) {
  return $attr;
}
if ( $meta['width'] && $meta['height'] ) {
  $width  = (int) $meta['width'];
  $height = (int) $meta['height'];
}

Change History (1)

#1 @SergeyBiryukov
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Summary changed from TwentyTwentyOne: twenty_twenty_one_get_attachment_image_attributes() missing wp_get_attachment_metadata() check to Twenty Twenty-One: twenty_twenty_one_get_attachment_image_attributes() missing wp_get_attachment_metadata() check

Hi there, welcome back to WordPress Trac!

Thanks for the report, this is already fixed for WordPress 5.9 in [52217] / #54464.

Note: See TracTickets for help on using tickets.