ARTICLE AD BOX
I deleted some unwanted thumbnail images from the Uploads directory on a WordPress site. However references to them had been added to the ‘srcset’ attribute of ‘img’ elements on various pages.
To remove these now unwanted references I used the code below in my child theme functions.php then re-loaded each page. The srcset was re-generated and the image reference removed. I changed the width size to target each size of thumbnails I didn’t need.
function avf_wp_calculate_image_srcset_mod( $sources ) { foreach ( $sources as $width => $image ) { if ( $width == 495 ) { unset( $sources[$width] ); } } return $sources; } add_filter( 'wp_calculate_image_srcset', 'avf_wp_calculate_image_srcset_mod' );This code worked fine for landscape thumbnails where $width was the common factor.
My problem is that I had some portrait thumbnails such as “image-xyw-123x400.jpg” where I need to target the 400px height aspect (and other heights).
So I changed $width to $height in the code – and it doesn’t work. I still have x400 thumbnails listed in srcset and, depending on screen size, the theme sometimes calls these up - and because they don’t exist anymore, I have missing images on some pages, hence why I need to remove the references.
Anyone any idea why $height is not working? Do I need to tweak the code in another way?
