phpwordpressimageresponsive-designsrcset

Remove certain sizes from srcset in Wordpress images


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');

Solution

  • 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:

    add_filter( 'intermediate_image_sizes_advanced', function( $new_sizes, $image_meta, $attachment_id ) {
        unset( $new_sizes['1536x1536'] );
        unset( $new_sizes['2048x2048'] );
        return $new_sizes;
    }, 10, 3 );