Make WordPress Core

Opened 10 months ago

Closed 10 months ago

Last modified 8 months ago

#58212 closed enhancement (fixed)

Improve clunky logic to inject `loading` attribute in the `get_the_post_thumbnail()` function

Reported by: flixos90's profile flixos90 Owned by: flixos90's profile flixos90
Milestone: 6.3 Priority: high
Severity: normal Version:
Component: Post Thumbnails Keywords: has-patch, has-unit-tests, add-to-field-guide, has-dev-note
Focuses: performance Cc:

Description

The get_the_post_thumbnail() function contains a somewhat hacky workaround today to compute the loading attribute (via wp_get_loading_attr_default(). While it uses wp_get_attachment_image() for basically all its markup, the loading attribute is already calculated before, which works technically okay, but has several drawbacks - most importantly there is no context on other attributes available in that function, and there is a risk of potentially calling the function again for the same image which would skew the content media count.

This logic should be changed so that the wp_get_loading_attr_default() function call from wp_get_attachment_image() is used, even when the latter is invoked by get_the_post_thumbnail(). We can apply use of the being_fetch_post_thumbnail_html and being_fetch_post_thumbnail_html actions to conditionally change the $context value passed to wp_get_loading_attr_default(), in a similar way to how they are already used today to add the wp-post-image CSS class to such image tags.

Change History (11)

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


10 months ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @flixos90
10 months ago

  • Priority changed from normal to high

Addressing the remaining lazy-loading related performance issues is a high priority for the performance team for the 6.3 cycle.

@flixos90 commented on PR #4390:


10 months ago
#3

@kt-12 FYI since you're working on some of the other lazy-load related tickets.

#4 @flixos90
10 months ago

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

@flixos90 commented on PR #4390:


10 months ago
#5

@kt-12 @spacedmonkey @westonruter I've replied to your feedback and made some updates.

#6 @flixos90
10 months ago

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

In 55821:

Media: Introduce wp_get_attachment_image_context filter.

Since WordPress 5.9, a "context" value of "wp_get_attachment_image" has been used in the wp_get_attachment_image() function to provide context to underlying functions where that is relevant, e.g. wp_get_loading_attr_default(). Since that value used to be not customizable, it required a workaround in get_the_post_thumbnail() to avoid calling those functions in wp_get_attachment_image(), which resulted in unnecessary complexity and was prone to errors.

This changeset introduces a wp_get_attachment_image_context filter and leverages it with private filter callback functions that are leveraged by default when get_the_post_thumbnail() is called. This avoids the need for the previous workaround and furthermore provides flexibility for other callers of wp_get_attachment_image() to provide their own contexts.

Props flixos90, costdev, thekt12, westonruter, spacedmonkey.
Fixes #58212.

#8 @flixos90
10 months ago

  • Keywords needs-dev-note added

#9 @stevenlinx
8 months ago

  • Keywords add-to-field-guide added

#11 @flixos90
8 months ago

  • Keywords needs-dev-note removed
Note: See TracTickets for help on using tickets.