Make WordPress Core


Ignore:
Timestamp:
05/17/2023 06:29:41 PM (2 years ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r55093 r55821  
    187187        }
    188188
    189         // Add `loading` attribute.
    190         if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
    191             // Get the 'loading' attribute value to use as default, taking precedence over the default from
    192             // `wp_get_attachment_image()`.
    193             $loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
    194 
    195             // Add the default to the given attributes unless they already include a 'loading' directive.
    196             if ( empty( $attr ) ) {
    197                 $attr = array( 'loading' => $loading );
    198             } elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
    199                 $attr['loading'] = $loading;
    200             } elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
    201                 $attr .= '&loading=' . $loading;
    202             }
    203         }
    204 
    205189        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
    206190
Note: See TracChangeset for help on using the changeset viewer.