Here’s an example for filtering out sizes above set limit from the srcset
attribute on certain post type. This works for featured image and images added to the post content with Gutenberg. (Tested on Twenty Twenty theme).
function filter_wp_calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
global $post;
if ( $post && 'post' === $post->post_type ) {
$max_size = 600; // change as needed
// var_dump($sources);
foreach ($sources as $size => $image) {
if ( $size > $max_size ) {
unset($sources[$size]);
}
}
// var_dump($sources);
}
return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'filter_wp_calculate_image_srcset', 11, 5 );
Filter docs: https://developer.wordpress.org/reference/hooks/wp_calculate_image_srcset/
Related Posts:
- Remove after images
- Images with overlay
- Missing feature image link function
- Add image size if page template
- How do I change/modify the_post_thumbnail(); html output?
- Set JPEG compression for specific custom image sizes
- remove links from images using functions.php
- Issue with wp_get_attachment_image() and SVG file type
- How to change the markup WordPress inserts for post images
- Programmatically Set First Image as Featured
- Removing default image size list in Media Box
- How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?
- WP 4.4. responsive loads normal image after loading the responsive image
- How to add classes to images based on their categories?
- How to create thumbnails for PDF uploads?
- ajax live search for post title
- How to reduce original image quality on upload?
- Is it possible to maintain image aspect ratio when scaling images?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- Set image size on media and text block with a function
- Wrap gutenberg block ‘div’ in other elements/extra HTML
- Generating Responsive Background Image Sizes in PHP
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Create thumbnail on PDF upload with Gravity Forms
- Gallery images titles – get from post
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- How to add a rel attribute to images that contains their categories?
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Pinterest Integration Using functions.php
- radio button is checked but display not check
- Display height and width properties with the_post_thumbnail() or related function
- Insert width & height attributes to all images displayed on each page
- Custom image size in Media Dropdown
- Best way to programatically add “rel” attributes to page and post images
- Removing the first image in content
- Function to auto-set a featured image that is already in use
- Actions according to image type and size
- Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- How to add css class to image attached in all the posts?
- Add custom image size not working
- Count total number of images in post and echo results as number
- how to add an image to the footer?
- wp_get_attachment_image function size argument not working if I also add a custom class
- Automatically wrap multiple images in div
- Inserting HTML tag with ACF into shortcode
- Understanding and altering the structure of posted images
- Overwriting Parent Theme’s Image Sizes
- Using Schema with `the_excerpt`
- Image rotation fails to regenerate custom sized thumbnail
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- Twentyten Child Theme: header images display messed up in admin panel
- Disable auto-resizing of uploaded images, but only for certain filename
- Display images that are not in the content
- Scripts not appending to element in AJAX call – why?
- WordPress Menuing Question :focus
- Can you put a condition on add_image_size() depending on the width of the uploaded image?
- show first 3 thumbnails of posts in different sizes [closed]
- Warning: : DOMDocument::loadHTML(): Empty string supplied as input in functions.php when adding classes to post images
- Getting custom image size URL in functions
- Set spesific size of featured images
- Can’t generate thumbnail images
- PHP mixed with some JS code to update WordPress theme settings
- media resize/crop possibilities?
- Add a unique class to HTML tag/element
- Make wp_link_pages() suitable for Twitter Bootstrap markup
- Control the srcset much more (not all sizes in it each time)
- Way to store featured image width and height in two separate variables?
- echo custom fields with AJAX
- srcset and custom field get_post_meta and wp_get_attachment_url
- Limit dimensions of “Full” size images
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- How to hide image-url if no attachment?
- What are the meta fields for an attachment?
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- The correct way to add a JavaScript in the functions.php [closed]
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- Need to convert image url to a Base_64 data url with wordpress function..
- How to Add Shortcode to html img code?
- Auto scroll to id on page load on all pages except home page. Only do this on mobile
- How to add a new image size and apply it to posts only?
- Function to insert missing image size attributes into img tags
- Image Crop Not Working
- Use existing image sizes for WooCommerce
- Problem in using Customizer
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- Retrieve First Image Function
- Creating multiple category drop down
- Custom Image Size in Insert Media
- Featured Image keeps cropping
- Prevent creating multiple image resizes in twentytwelve template
- Shortcode of a function
- Conditional function for excluding first image from content, not working
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute