Skip to content

Commit 65caf62

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55093 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 84bb54d commit 65caf62

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/wp-includes/post-thumbnail-template.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,20 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr =
186186
update_post_thumbnail_cache();
187187
}
188188

189-
// Get the 'loading' attribute value to use as default, taking precedence over the default from
190-
// `wp_get_attachment_image()`.
191-
$loading = wp_get_loading_attr_default( 'the_post_thumbnail' );
192-
193-
// Add the default to the given attributes unless they already include a 'loading' directive.
194-
if ( empty( $attr ) ) {
195-
$attr = array( 'loading' => $loading );
196-
} elseif ( is_array( $attr ) && ! array_key_exists( 'loading', $attr ) ) {
197-
$attr['loading'] = $loading;
198-
} elseif ( is_string( $attr ) && ! preg_match( '/(^|&)loading=/', $attr ) ) {
199-
$attr .= '&loading=' . $loading;
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+
}
200203
}
201204

202205
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

tests/phpunit/tests/post/thumbnails.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,41 @@ public function test_post_thumbnail_size_filter_complex( $which_post, $expected
408408
$this->assertSame( $expected, $result );
409409
}
410410

411+
/**
412+
* @ticket 57490
413+
*/
414+
public function test_get_the_post_thumbnail_includes_loading_lazy() {
415+
set_post_thumbnail( self::$post, self::$attachment_id );
416+
417+
$html = get_the_post_thumbnail( self::$post );
418+
$this->assertStringContainsString( ' loading="lazy"', $html );
419+
}
420+
421+
/**
422+
* @ticket 57490
423+
*/
424+
public function test_get_the_post_thumbnail_respects_passed_loading_attr() {
425+
set_post_thumbnail( self::$post, self::$attachment_id );
426+
427+
$html = get_the_post_thumbnail( self::$post, 'post-thumbnail', array( 'loading' => 'eager' ) );
428+
$this->assertStringContainsString( ' loading="eager"', $html, 'loading=eager was not present in img tag because attributes array with loading=eager was overwritten.' );
429+
430+
$html = get_the_post_thumbnail( self::$post, 'post-thumbnail', 'loading=eager' );
431+
$this->assertStringContainsString( ' loading="eager"', $html, 'loading=eager was not present in img tag because attributes string with loading=eager was overwritten.' );
432+
}
433+
434+
/**
435+
* @ticket 57490
436+
*/
437+
public function test_get_the_post_thumbnail_respects_wp_lazy_loading_enabled_filter() {
438+
set_post_thumbnail( self::$post, self::$attachment_id );
439+
440+
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
441+
442+
$html = get_the_post_thumbnail( self::$post );
443+
$this->assertStringNotContainsString( ' loading="lazy"', $html );
444+
}
445+
411446
public function data_post_thumbnail_size_filter_complex() {
412447
return array(
413448
array( 0, 'medium' ),

0 commit comments

Comments
 (0)