Make WordPress Core

Opened 2 years ago

Closed 23 months ago

Last modified 22 months ago

#57086 closed enhancement (fixed)

Omit or filter decoding attribute in wp_get_attachment_image()

Reported by: maximej's profile maximej Owned by: flixos90's profile flixos90
Milestone: 6.2 Priority: normal
Severity: normal Version: 6.1
Component: Media Keywords: has-patch has-unit-tests commit add-to-field-guide needs-dev-note
Focuses: performance Cc:

Description

With that changeset [53480], decoding="async" is added by default to images from wp_get_attachment_image()

And there are no way to :

  • either remove the decoding attribute

Unlike the loading attribute, passing decoding false in $attr of wp_get_attachment_image wouldn't omit it.

  • or filtering it

the newly introduced filter wp_img_tag_add_decoding_attr isn't treated here

In my case, I need to disable this and right now, I see no other options than overriding specific plugins functions or modifying media core file.

(I manage a website where this async decoding attribute causes erratic issues in which some images of the posts carousels are not loaded until a browser zoom or some other action).

Attachments (3)

57086.diff (4.2 KB) - added by adamsilverstein 2 years ago.
57086.2.diff (3.9 KB) - added by adamsilverstein 2 years ago.
57086.3.diff (3.9 KB) - added by adamsilverstein 2 years ago.

Download all attachments as: .zip

Change History (19)

#1 @maximej
2 years ago

Mentioning original ticket/discussion as well #53232

I agree that issues are very unlikely to happen but I would have expected more control on it.
Moreover, some might prefer a minimalistic/by default HTML markup.

#2 follow-up: @peterwilsoncc
2 years ago

  • Focuses performance added
  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 6.2

Hi @maximej and welcome back to trac!

The decoding attribute can be controlled via the wp_img_tag_add_decoding_attr filter by returning a value of false, you can see further documentation for the filter in the developer guide.

You can also control the default value by passing either false, "async", "auto" (the default value browsers use) or "sync" as an attribute:

<?php
echo wp_get_attachment_image( 19, 'thumbnail', false, array( 'decoding' => 'auto' ) );

However, passing false displays the string as an empty attribute, decoding="", which I believe is a bug that ought to be fixed and the attribute removed from the HTML generated.

I've moved this on to the 6.2 milestone for visibility and added the performance focus as the bug was introduced in an earlier performance ticket.

This ticket was mentioned in PR #3633 on WordPress/wordpress-develop by @adamsilverstein.


2 years ago
#3

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

#4 @adamsilverstein
2 years ago

57086.diff offers an approach that avoids adding the decoding attribute when passed as false or an empty string and also adds tests.

#5 in reply to: ↑ 2 @maximej
2 years ago

Hi @peterwilsoncc, thanks for the consideration !

The decoding attribute can be controlled via the wp_img_tag_add_decoding_attr filter by returning a value of false, you can see further documentation for the filter in the developer guide.

Unless I miss something, that filter has no effect on wp_get_attachment_image()
(in my tests and by seeing the code)

The patch submitted handles only the first part of the ticket.

Shouldn't we introduce that filter in that function too ?

#6 @adamsilverstein
2 years ago

Shouldn't we introduce that filter in that function too ?

@maximej You should be able to use the wp_get_attachment_image_attributes filter that runs later in this function to remove the attribute*. Adding another filter seems redundant.

Is the wp_get_attachment_image function being called in a plugin you use or your own code?

(I manage a website where this async decoding attribute causes erratic issues in which some images of the posts carousels are not loaded until a browser zoom or some other action).

Can you elaborate on that a bit further? my understanding was the decoding="async" attribute is safe to use in every context. If not, I'd like to better understand the underlying issue. What code or plugin do you use to show post carousels? Do the issues happen in specific browsers or browser versions? can you share a link to a problematic URL?

#7 @adamsilverstein
2 years ago

  • Keywords reporter-feedback added

@flixos90 commented on PR #3633:


2 years ago
#8

@adamsilverstein This PR is very close to commit. Any chance you can get back to it in the next few weeks so we can get it committed?

#9 @flixos90
23 months ago

  • Owner set to flixos90
  • Status changed from new to reviewing

@flixos90 commented on PR #3633:


23 months ago
#10

@peterwilsoncc Would you be available for taking another look at this as well?

#11 @flixos90
23 months ago

  • Keywords commit added; reporter-feedback removed

#12 @flixos90
23 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 55171:

Media: Allow to omit decoding="async" on tags from wp_get_attachment_image().

When adding decoding="async" to images was introduced in [53480], it did not provide the ability to customize that behavior specifically for image tags returned from wp_get_attachment_image().

With this changeset it is now possible to explicitly provide a decoding value of e.g. boolean false in the $attr parameter of the function, to ensure the attribute is omitted.

Props maximej, adamsilverstein, flixos90, costdev, peterwilsoncc, mukesh27.
Fixes #57086.

#14 @milana_cap
22 months ago

  • Keywords add-to-field-guide added

#15 @adamsilverstein
22 months ago

  • Keywords needs-dev-note added

This ticket was mentioned in Slack in #core-media by adamsilverstein. View the logs.


22 months ago

Note: See TracTickets for help on using tickets.