Media: Allow for customization of lazy-loading featured images.

When lazy-loading images was introduced, in [52065] the check for `wp_lazy_loading_enabled()` was omitted by accident in the logic to set the attribute with its default value on `img` tags from `get_the_post_thumbnail()`. Without this check, it is impossible for third-party developers to modify the behavior for featured images.

This changeset fixes the problem by introducing the check.

Props flixos90, joemcgill, mukesh27.
Fixes #57490.

Built from https://develop.svn.wordpress.org/trunk@55093


git-svn-id: http://core.svn.wordpress.org/trunk@54626 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-01-19 07:34:14 +00:00
parent 18b551b84d
commit 1a21c14d85
2 changed files with 14 additions and 11 deletions

View File

@ -186,17 +186,20 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr =
update_post_thumbnail_cache();
}
// Get the 'loading' attribute value to use as default, taking precedence over the default from
// `wp_get_attachment_image()`.
$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
// Add `loading` attribute.
if ( wp_lazy_loading_enabled( 'img', 'the_post_thumbnail' ) ) {
// Get the 'loading' attribute value to use as default, taking precedence over the default from
// `wp_get_attachment_image()`.
$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
// Add the default to the given attributes unless they already include a 'loading' directive.
if ( empty( $attr ) ) {
$attr = array( 'loading' => $loading );
} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
$attr['loading'] = $loading;
} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
$attr .= '&loading=' . $loading;
// Add the default to the given attributes unless they already include a 'loading' directive.
if ( empty( $attr ) ) {
$attr = array( 'loading' => $loading );
} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
$attr['loading'] = $loading;
} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
$attr .= '&loading=' . $loading;
}
}
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55092';
$wp_version = '6.2-alpha-55093';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.