From e960ee94b707b2d1bef5eec484de9aa2b0a16e95 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 12 Sep 2024 12:57:15 -0400 Subject: [PATCH] Homepage: Prevent small images from being used on hero image --- themes/wporg-5ftf-2024/functions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/themes/wporg-5ftf-2024/functions.php b/themes/wporg-5ftf-2024/functions.php index 752d9bf..09a62c3 100644 --- a/themes/wporg-5ftf-2024/functions.php +++ b/themes/wporg-5ftf-2024/functions.php @@ -23,6 +23,7 @@ add_filter( 'the_content', __NAMESPACE__ . '\inject_pledge_content', 1 ); add_filter( 'get_the_excerpt', __NAMESPACE__ . '\inject_pledge_content', 1 ); add_filter( 'search_template_hierarchy', __NAMESPACE__ . '\use_archive_template' ); add_filter( 'body_class', __NAMESPACE__ . '\add_body_class' ); +add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\modify_image_srcset', 10, 3 ); // Remove table of contents. add_filter( 'wporg_handbook_toc_should_add_toc', '__return_false' ); @@ -127,3 +128,23 @@ function swap_avatar_for_featured_image( $html, $post_id, $post_thumbnail_id, $s return $html; } add_filter( 'post_thumbnail_html', __NAMESPACE__ . '\swap_avatar_for_featured_image', 10, 5 ); + +/** + * Filter the sizes attributes for specific images. + * + * @param array $sources One or more arrays of source data to include in the 'srcset'. + * @param array $size_array An array of requested width and height values. + * @param string $image_src The 'src' of the image. + * + * @return array + */ +function modify_image_srcset( $sources, $size_array, $image_src ) { + // The main header image is set with a fixed height, and when the smaller + // screen sizes are used, it scales up to that height causing pixelation. + if ( str_contains( $image_src, '5ftf-wordcamp.jpg' ) ) { + // These sizes are smaller than 260px tall. + unset( $sources[1024], $sources[768], $sources[300] ); + } + + return $sources; +}