as we all know since WordPress 4.4 there are responsive images sizes. My goal is to remove 2 sizes from generation and even from displaying in the srcset of the images. For some reason even after using the following code, the 2 sizes still displayed in the srcset! Any solutions maybe? 🙂
add_action( 'init', 'j0e_remove_large_image_sizes' );
function j0e_remove_large_image_sizes() {
remove_image_size( '1536x1536' ); // 2 x Medium Large (1536 x 1536)
remove_image_size( '2048x2048' ); // 2 x Large (2048 x 2048)
}
function remove_default_image_sizes( $sizes) {
unset( $sizes['1536×1536']);
unset( $sizes['2048×2048']);
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_default_image_sizes');
2
Answers
The
intermediate_image_sizes_advanced
will work, but it looks like you’ve set the wrong keys there. The×
is the wrong character.The following should work fine:
hook into
wp_calculate_image_srcset
in wp-includes/media.php:1061-1180; read detailed explanation$sources
is an array that holds all available sizes for thesrcset
attribute. You can unset undesired sizes as shown below.