[wp-trac] [WordPress Trac] #54701: TwentyTwentyOne: twenty_twenty_one_get_attachment_image_attributes() missing wp_get_attachment_metadata() check

WordPress Trac noreply at wordpress.org
Mon Dec 27 19:18:04 UTC 2021


#54701: TwentyTwentyOne: twenty_twenty_one_get_attachment_image_attributes()
missing wp_get_attachment_metadata() check
---------------------------+-----------------------------
 Reporter:  justlevine     |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Bundled Theme  |    Version:  5.8.2
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 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
 <?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
 <?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'];
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54701>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list