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
- How do I change/modify the_post_thumbnail(); html output?
- Set JPEG compression for specific custom image sizes
- 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
- 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?
- 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
- Generating Responsive Background Image Sizes in PHP
- Create thumbnail on PDF upload with Gravity Forms
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- Pinterest Integration Using functions.php
- 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
- 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
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- Disable auto-resizing of uploaded images, but only for certain filename
- Display images that are not in the content
- 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
- Can’t generate thumbnail images
- PHP mixed with some JS code to update WordPress theme settings
- 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)
- srcset and custom field get_post_meta and wp_get_attachment_url
- How to hide image-url if no attachment?
- What are the meta fields for an attachment?
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- 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
- 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
- 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
- Different image using srcset function
- Is it possible to limit number of files a non-admin user can upload via the WordPress media library?
- Auto delete content in specific folder inside media library
- How to change images url in function.php?
- How to use data URL in WordPress?
- How to update an image attachment’s alt text from a custom field when saving a post?
- how to check if an element in an html file exists in another html file? [closed]
- Class parameter in get_avatar args doesn’t get added to output
- large image size of 1440×1440 not pulling 2x of 2880×2880 image
- Can You Set A Minimum Image Dimension For Resizing Images?
- How do I make a custom “Read More Blogs” button for my blog page
- (Divi): How to make WordPress load images of specific size for Blurb modules on given page with a funciton?
- Add schema to Search result page’s body. Tried using is_search
- Featured image on archive page based on post type
- Printing medium sized image
- restrict uploaded image size and fixed image display size
- Using Switch Statement to Change Image According to Last Digit of Topic ID
- Disable links to images only if link is an image
- Some code is added automatically to my site’s header – what is it?
- Restricted images only for logged user. (hidden url)
- Zip all (original) images from media gallery
- Automatic add space if user enters number(any digit)
- Different body image backgrounds on different pages, posts and categories
- Open image size links in a page instead of direct image link
- Function to get thumbnail img source
- Image width issue in IE [closed]
- image_size with respect to aspect ratios