2

WordPress's automatic responsive image code is setting the size of certain images larger than it should be. Here is an example:

How can I change the responsive image code being generated from WordPress to change, so the image can never be set to a size larger than it's default size? All images are different dimensions, so I won't be able to hard-code a width into the code.

Any help or direction to resources would be much appreciated.

6
  • Now that you mention it, I also wondered about it this week. A page I found: make.wordpress.org/core/2015/11/10/… Hopefully someone can help us out. Commented Apr 9, 2016 at 0:31
  • I'm not able to reproduce your problem using the default themes. Might it be something related to yours? In any case, you can have a look at max_srcset_image_width. In the comments there is a solution that might work for you.
    – Luis Sanz
    Commented Apr 9, 2016 at 8:28
  • Thanks for your help @LuisSanz. I looked into both of these options. They still seem to require hard-coding a width, which won't really work since it needs to be more dynamic.
    – Andrew
    Commented Apr 11, 2016 at 15:46
  • @Andrew, the idea is to set some logic in the filter. You can access the images $size_array to compare each on an individual basis. I would help you gladly if I were able to reproduce the problem.
    – Luis Sanz
    Commented Apr 11, 2016 at 17:29
  • @LuisSanz, okay, I think I figured out the issue. I posted the answer below.
    – Andrew
    Commented Apr 11, 2016 at 17:57

1 Answer 1

0

The issue I was having was with some conflicting code I had in my functions file (see below). I usually use this code to remove width/height attributes from being hard-coded into the element in the content area. Removing these somehow appears to mess up the new responsive image implementation in WordPress though. As soon as I removed this code, the responsive images appear to be working as expected.

Conflicting Code

// Remove Width & Height Attributes from Inserted Images
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.