Make WordPress Core

Opened 4 months ago

Closed 6 weeks ago

Last modified 6 weeks ago

#61436 closed defect (bug) (fixed)

'src' not in $attr for wp_get_loading_optimization_attributes

Reported by: prestonwordsworth's profile prestonwordsworth Owned by: flixos90's profile flixos90
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.4
Component: Media Keywords: has-patch has-unit-tests
Focuses: performance Cc:

Description

The relevant dev note has the following snippet:

function set_fetchpriority_high_on_specific_image( $loading_attrs, $tag_name, $attr, $context ) {
    if ( 'img' === $tag_name ) {
        if (
            'the_content' === $context &&
            isset( $attr['src'] ) &&
            $attr['src'] === 'https://example.org/a-specific-image.jpg'
        ) {
            $loading_attrs['fetchpriority'] = 'high';
        } else {
            unset( $loading_attrs['fetchpriority'] );
        }
    }
    return $loading_attrs;
}
add_filter(
    'wp_get_loading_optimization_attributes',
    'set_fetchpriority_high_on_specific_image',
    10,
    4
);

'src', however, hasn’t made it into $attr array yet:

        $optimization_attrs = wp_get_loading_optimization_attributes(
                'img',
                array(
                        'width'         => $width,
                        'height'        => $height,
                        'loading'       => $loading_val,
                        'fetchpriority' => $fetchpriority_val,
                        'decoding'      => $decoding_val,
                ),
                $context
        );

Change History (15)

#1 @joemcgill
4 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Version changed from 6.5.4 to 6.4

Hi @prestonwordsworth, welcome to Trac!

The main place where attributes are set up for this filter are in wp_img_tag_add_loading_optimization_attrs(), which does not pass the src nor the actual image to wp_get_loading_optimization_attributes() where the filter is called. There are other places where the src is passed, like in get_header_image_tag() or WP_Widget_Media_Image::render_media, but it probably makes sense to make this consistent.

Thanks for the report!

#2 @prestonwordsworth
4 months ago

Many thanks for confirming that this is a case, @joemcgill!

So is it currently possible to change the fetchpriority or decoding of a specific image using a filter?

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


4 months ago
#3

  • Keywords has-patch added

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


3 months ago

#5 @adamsilverstein
3 months ago

  • Keywords needs-unit-tests added

#6 @adamsilverstein
3 months ago

A unit test to validate would be nice to have

#7 @joemcgill
3 months ago

  • Focuses docs removed
  • Milestone changed from Future Release to 6.7

Thanks for the PR, @deepakrohilla! This looks pretty good. I'm going to add this to the 6.7 milestone and get a proper code review. It will be great to add unit tests for this.

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


3 months ago
#8

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

Trac ticket:

#9 @deepakrohilla
3 months ago

Thanks @joemcgill I have added test cases in PR ,Please review 

@mukesh27 commented on PR #6830:


6 weeks ago
#10

@deepakrohillas Left some suggestion other then it looks good to me.

#11 @mukesh27
6 weeks ago

  • Keywords changes-requested added

@deepakrohilla, I’ve left some feedback on the PR!

@adamsilverstein or @joemcgill, could one of you please assign yourself for the owner?

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


6 weeks ago

#13 @flixos90
6 weeks ago

  • Keywords changes-requested removed
  • Owner set to flixos90
  • Status changed from new to reviewing

#14 @flixos90
6 weeks ago

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

In 58974:

Media: Consistently pass 'src' attribute to wp_get_loading_optimization_attributes().

A common use-case for the 'wp_get_loading_optimization_attributes' filter is to modify attributes based on the 'src' attribute. However, the wp_img_tag_add_loading_optimization_attrs() was not passing that attribute to the function as expected, which would make such usage of the filter unreliable. This changeset ensures the 'src' attribute is also passed in this scenario. All other calls to wp_get_loading_optimization_attributes() already included the attribute.

Props deepakrohilla, prestonwordsworth, mukesh27, adamsilverstein, joemcgill, flixos90.
Fixes #61436.
See #58893.

Note: See TracTickets for help on using tickets.