• Guys I am pulling my hair out on this. Just want to remove the sizes attribute but keep the srcset attribute on all my IMG tags.

    Something like this would work fine:
    return preg_replace( ‘/sizes=”(.*)”/’, ”, $html );

    BUT the sizes and srcset is generated upon page rendering.

    Please help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The sizes should be generated when the media library sends the image to the editor. In that case hook ‘image_send_to_editor’ and apply your preg_replace() to the passed HTML.

    If it is indeed done on page rendering, this is an unusual situation. You’d have to locate the code that does the output to know what to do. On your theme’s templates perhaps?

    Thread Starter simplywallst

    (@simplywallst)

    Yes it happens here:
    https://developer.ww.wp.xz.cn/reference/functions/wp_calculate_image_sizes/

    // Setup the default ‘sizes’ attribute.
    $sizes = sprintf( ‘(max-width: %1$dpx) 100vw, %1$dpx’, $width );

    Thread Starter simplywallst

    (@simplywallst)

    I solved it for anyone interested.. not elegant but $sizes doesn’t work if its null.

    function remove_image_size_attributes( $sizes) {

    return str_replace(“insert max size here”,”1600″,$sizes);
    }

    // Remove image size attributes from post thumbnails
    add_filter( ‘wp_calculate_image_sizes’, ‘remove_image_size_attributes’ );

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘remove sizes attribute in IMG’ is closed to new replies.