13

The WordPress 4.4 adds multiple image sizes with srcset when using the_post_thumbnail function. Is it possible to get only one image size without srcset ?

I understand that it is possible to add a filter to disable srcset from all images, but I want to disable the srcset only when calling a specific thumbnail size (for example only when calling full image size).

2 Answers 2

14

I want to disable the srcset only when calling a specific thumbnail size (for example only when calling full image size).

Here are two ideas (if I understand you correctly):

Approach #1

Let's check the size from the post_thumbnail_size filter. If it matches a corresponding size (e.g. full) then we make sure the $image_meta is empty, with the wp_calculate_image_srcset_meta filter. That way we can bail out early from the wp_calculate_image_srcset() function (earlier than using the max_srcset_image_width or wp_calculate_image_srcset filters to disable it):

/**
 * Remove the srcset attribute from post thumbnails 
 * that are called with the 'full' size string: the_post_thumbnail( 'full' )
 *
 * @link http://wordpress.stackexchange.com/a/214071/26350
 */
 add_filter( 'post_thumbnail_size', function( $size )
 {
     if( is_string( $size ) && 'full' === $size )
         add_filter( 
             'wp_calculate_image_srcset_meta',  
              '__return_null_and_remove_current_filter' 
         );   
    return $size;
 } );

// Would be handy, in this example, to have this as a core function ;-)
function __return_null_and_remove_current_filter ( $var )
{
    remove_filter( current_filter(), __FUNCTION__ );
    return null;
}

If we have:

the_post_thumbnail( 'full' );

then the generated <img> tag will not contain the srcset attribute.

For the case:

the_post_thumbnail();

we could match the 'post-thumbnail' size string.

Approach #2

We could also add/remove the filter manually with:

// Add a filter to remove srcset attribute from generated <img> tag
add_filter( 'wp_calculate_image_srcset_meta', '__return_null' );

// Display post thumbnail
the_post_thumbnail();

// Remove that filter again
remove_filter( 'wp_calculate_image_srcset_meta', '__return_null' );
7
  • you probably need to also remove the wp_calculate_image_srcset_meta filter when the function ends Jan 8, 2016 at 14:43
  • I added the approach where we add/remove the filter callback manually. I use the instance counting in the other approach to run it only once for each the_post_thumbnail() call @MarkKaplun
    – birgire
    Jan 8, 2016 at 15:02
  • I simplified the code snippet, thanks for letting me think about this again ;-) @MarkKaplun
    – birgire
    Jan 8, 2016 at 15:48
  • 1
    :) I just found a productive way to show my dislike to anonymous functions in filters :) Jan 8, 2016 at 15:51
  • 1
    or there should be a "run one time" parameter to add_filter. This pattern is really common. Jan 8, 2016 at 16:09
1

No filters are necessary. The most simple solution is below:

// exclude any attribute you want to keep from the list
// choose any defined image size, ie. large, medium, etc
echo preg_replace( '/(width|height|srcset|sizes)="[^"]*"/', '', get_the_post_thumbnail( get_the_ID(), 'large') );
1
  • This is perfect if you just want to disable the srcset in a single location in your theme. But if you want to get rid of it everywhere, using the filter seems a neater solution. Feb 16 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.