• Resolved Jon Scaife

    (@jonscaife)


    Hi Hector

    I use my theme thumbnail for the widget rather than the built-in thumbail functionality (via the wpp_parse_custom_content_tags filter). I just use a simple get_the_post_thumbnail($post_id, 'my-size') to generate the html output. This outputs all the normal attributes that it outputs elsewhere (e.g. title and srcset etc) however the sizes attribute doesn’t appear. Even if I try to force one by using get_the_post_thumbnail($post_id, 'my-size', array('sizes' => '40px')) it doesn’t appear in the eventual output. I can add any other attribute that I want (e.g. get_the_post_thumbnail($post_id, 'my-sizes', array('data-sizes' => '40px')) works as expected and I get a data-sizes attribute output to the page.

    So it looks like something is somehow sanitizing or otherwise removing the sizes attribute. This attribute does work on all the other image displays on my site so it isn’t a site-wide problem, it only doesn’t work within WPP as far as I can tell. Do you have any ideas what could be causing this, or any suggestions for how I might fix it? I did try using the wpp_post filter instead but the problem still remains with that too.

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @jonscaife, it’s been a while!

    Correct, WordPress Popular Posts is sanitizing HTML output via wp_kses() before rendering it on screen (see here and here.)

    For this, WPP relies on wp_kses_allowed_html() to determine what should stay and what should go. If you have a look at WordPress’ source code you’ll notice that ‘sizes’ isn’t part of the allowed HTML attributes for images which is why it’s is being automatically removed from your thumbnails.

    Fortunately, wp_kses_allowed_html() provides a filter hook called wp_kses_allowed_html that allows you to modify what should be allowed / removed. From memory (I didn’t test so please adjust where/if needed) you want something like this:

    function wp6523_allow_sizes_attribute_in_images( $tags, $context ) {
        if ( 'post' == $context ) {
            $tags['img']['sizes'] = true;
        }
        return $tags;
    }
    add_filter( 'wp_kses_allowed_html', 'wp6523_allow_sizes_attribute_in_images', 20, 2 );
    • This reply was modified 10 months, 1 week ago by Hector Cabrera. Reason: Fixed typo in code
    Thread Starter Jon Scaife

    (@jonscaife)

    Amazing support as always, that did the trick straight away. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘no “sizes” attribute for images?’ is closed to new replies.