最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - Remove image of srcset

matteradmin6PV0评论

Some wordpress plugins creates new thumbnail sizes which I don't need.

I have created a filter

add_filter( 'intermediate_image_sizes_advanced', 

// Remove default image sizes here.
function prefix_remove_default_images( $sizes ) {
    unset( $sizes['sow-carousel-default']); // 272px x 182px set by widgets for siteorigin (carousel)
    return $sizes;
}

to unset these new thumbnail sizes and doesn't create these images. So that works.

But the data-srcset

<img width="270" height="180" src="data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PScwIDAgMjcwIDE4MCcgeG1sbnM9J2h0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnJz48L3N2Zz4=" class="attachment-post-thumbnail size-post-thumbnail lazyload wp-post-image" alt="Ballonnen met gas gevuld aan plafond" title="Ballonnen voor je huwelijk kopen" sizes="(max-width: 270px) 100vw, 270px" data-srcset="/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-270x180.jpg 270w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-600x400.jpg 600w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-300x200.jpg 300w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-768x512.jpg 768w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-1024x682.jpg 1024w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-770x515.jpg 770w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-272x182.jpg 272w, /wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-110x73.jpg 110w" data-src="/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-270x180.jpg" />

is still looking for this dimension (in this particular case 272 x 182) and it's giving me a 404, because the image is not generated.

How can I disable this particular image from this data-srcset?

I only see how I can hide the data-srcset, but that hides them all:

add_filter( 'wp_calculate_image_srcset_meta', '__return_empty_array' );

I only want to get rid of specific thumbnails. Is that oossible??

Some wordpress plugins creates new thumbnail sizes which I don't need.

I have created a filter

add_filter( 'intermediate_image_sizes_advanced', 

// Remove default image sizes here.
function prefix_remove_default_images( $sizes ) {
    unset( $sizes['sow-carousel-default']); // 272px x 182px set by widgets for siteorigin (carousel)
    return $sizes;
}

to unset these new thumbnail sizes and doesn't create these images. So that works.

But the data-srcset

<img width="270" height="180" src="data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PScwIDAgMjcwIDE4MCcgeG1sbnM9J2h0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnJz48L3N2Zz4=" class="attachment-post-thumbnail size-post-thumbnail lazyload wp-post-image" alt="Ballonnen met gas gevuld aan plafond" title="Ballonnen voor je huwelijk kopen" sizes="(max-width: 270px) 100vw, 270px" data-srcset="http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-270x180.jpg 270w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-600x400.jpg 600w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-300x200.jpg 300w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-768x512.jpg 768w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-1024x682.jpg 1024w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-770x515.jpg 770w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-272x182.jpg 272w, http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-110x73.jpg 110w" data-src="http://wpdyw.localhost/wp-content/uploads/2017/10/ballonnen-met-gas-gevuld-aan-plafond-270x180.jpg" />

is still looking for this dimension (in this particular case 272 x 182) and it's giving me a 404, because the image is not generated.

How can I disable this particular image from this data-srcset?

I only see how I can hide the data-srcset, but that hides them all:

add_filter( 'wp_calculate_image_srcset_meta', '__return_empty_array' );

I only want to get rid of specific thumbnails. Is that oossible??

Share Improve this question asked Mar 24, 2019 at 20:34 sat_usersat_user 31 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you look in core, that filter is applied like so:

$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );

Instead of using __return_empty_array you need to add a custom function to that filter to remove the srcset size you are trying to get rid of. Something like this.

function my_srcset_function($image_meta) {
    if (!is_array($image_meta)) {
        return $image_meta;
    }
    if (!empty($image_meta['sizes']) && !empty($image_meta['sizes']['sow-carousel-default'])) {
        unset($image_meta['sizes']['sow-carousel-default']);
    }
    return $image_meta;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far