8000 filter_post_thumbnail_sizes_attr() seems to prevent custom 'sizes' attribute being set when using native WP image functions · Issue #712 · wprig/wprig · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
filter_post_thumbnail_sizes_attr() seems to prevent custom 'sizes' attribute being set when using native WP image functions #712
Open
@briainod

Description

@briainod

Issue Overview

The filter_post_thumbnail_sizes_attr() function in inc/Image_sizes/Component.php looks like it's blocking the WordPress image functions' ability to set a custom attribute and always comes out at '100vw'

Steps to Reproduce

  1. Use any native WP function which outputs an image tag
  2. In the $attr parameter pass 'sizes' into the array with any value

Expected Behavior

'sizes' attribute on the image tag on the frontend should match the one passed to the $attr array
e.g.
wp_get_attachment_image($id, 'thumbnail', false, array('sizes' => '(max-width: 768px) 100vw, 20vw'))
should produce
<img width="250" height="250" src="http://mysite.com/wp-content/uploads/2021/04/test-1-250x250.png" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" sizes="(max-width: 768px) 100vw, 20vw" srcset="http://mysite.com/wp-content/uploads/2021/04/test-1-250x250.png 250w, http://mysite.com/wp-content/uploads/2021/04/test-1-360x360.png 360w, http://mysite.com/wp-content/uploads/2021/04/test-1-500x500.png 500w">

Current Behavior

'sizes' attribute on the image is always '100vw'
<img width="250" height="250" src="http://mysite.com/wp-content/uploads/2021/04/test-1-250x250.png" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" sizes="100vw" srcset="http://mysite.com/wp-content/uploads/2021/04/test-1-250x250.png 250w, http://mysite.com/wp-content/uploads/2021/04/test-1-360x360.png 360w, http://mysite.com/wp-content/uploads/2021/04/test-1-500x500.png 500w">

Possible Solution

Change 'inc/Image_Sizes/Component.php:85' from
$attr['sizes'] = '100vw';
to
if( !$attr['sizes']){ $attr['sizes'] = '100vw'; }

This seemed to work for me, may need more extensive testing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0