[wp-trac] [WordPress Trac] #45676: wp_get_attachment_img_srcset() ignores $size, and returns all $image_meta.

WordPress Trac noreply at wordpress.org
Mon Dec 17 16:27:05 UTC 2018


#45676: wp_get_attachment_img_srcset() ignores $size, and returns all $image_meta.
-----------------------------+-----------------------------
 Reporter:  justlevine       |      Owner:  (none)
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Post Thumbnails  |    Version:  5.0.1
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 The expected behavior for calling
 `wp_get_attachment_img_srcset($attachment_id, $size)` would be to return
 an `srcset` for all images up to and including {{{$size['width']}}}.

 Currently, it returns an `srcset` of ''all the `$sizes` registered to the
 attachment'' (that are less than `$max_srcset_image_width`).

 This occurs because the `$image_meta` that is passed to
 `wp_calculate_image_srcset()` is just `wp_get_attachment_metadata(
 $attachment_id )`, and any sizes larger than that aren't filtered out.

 === Suggested Fix
 The suggested fix is to filter out all the values in `$image_meta` whose
 widths are greater than {{{$size['width']}}}.

 I'm not comfortable with the coding standards or svn to submit a pr, but
 here's a possible way to code it: ({{{wp-includes/media.php, line 1020}}})

 {{{
 #!php marks=1020,1025
 if ( ! is_array( $image_meta ) ) {
   foreach ( $image_meta['sizes'] as $size_name->$meta) {
     if ( $image_meta['width'] > $size['width'] )
       unset( $image_meta['sizes']['size_name'] );
   }
 }
 }}}

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


More information about the wp-trac mailing list