[wp-trac] [WordPress Trac] #46885: Improvements/Issues with srcsets, additions to wp_calculate_image_srcset

WordPress Trac noreply at wordpress.org
Sat Apr 13 06:47:10 UTC 2019


#46885: Improvements/Issues with srcsets, additions to wp_calculate_image_srcset
-------------------------+------------------------------
 Reporter:  pinktank     |       Owner:  joemcgill
     Type:  enhancement  |      Status:  assigned
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Media        |     Version:  5.1
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by pinktank):

 I gave it a shot, no ide aif this is anywhere near correct because I am
 not that great with php.

 {{{#!php

 function filter_wp_calculate_image_srcset( $sources, $size_array,
 $image_src, $image_meta, $attachment_id ) {
         // Parse through array to unset the original src
         foreach($sources as $element_width => $element_source) {
                 if($element_source == $image_src ){
                     unset($sources[$element_width]);
                 }
         }

         // Move largest derivative to first position.
         $largest_img_key = max(array_keys($sources));
         $sources = array( $largest_img_key => $sources[$largest_img_key])
 + $sources;

         //Return modified sources
         return $sources;
 };

 // add the filter
 add_filter( 'wp_calculate_image_srcset',
 'filter_wp_calculate_image_srcset', 10, 5 );

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46885#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list