Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#61436 closed defect (bug) (fixed)

'src' not in $attr for wp_get_loading_optimization_attributes

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

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
2 years ago

  • Milestone Awaiting ReviewFuture Release
  • Version 6.5.46.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
2 years 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.


2 years ago
#3

  • Keywords has-patch added

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


2 years ago

#5 @adamsilverstein
2 years ago

  • Keywords needs-unit-tests added

#6 @adamsilverstein
2 years ago

A unit test to validate would be nice to have

#7 @joemcgill
2 years ago

  • Focuses docs removed
  • Milestone Future Release6.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.


2 years ago
#8

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

Trac ticket:

#9 @deepakrohilla
2 years ago

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

@mukesh27 commented on PR #6830:


2 years ago
#10

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

#11 @mukesh27
2 years 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.


2 years ago

#13 @flixos90
2 years ago

  • Keywords changes-requested removed
  • Owner set to flixos90
  • Status newreviewing

#14 @flixos90
2 years ago

  • Resolutionfixed
  • Status reviewingclosed

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.