Last active
October 26, 2022 15:02
-
-
Save westonruter/6adb64e6e8c858a40ac1dc51b03f16d8 to your computer and use it in GitHub Desktop.
Enable preload links for responsive hero images which have srcset and sizes attributes. This is disabled by default because only Chrome supports imagesrcset and imagesizes on preload links. This eliminates the Optimizer from raising this notice: CannotPreloadImage: Not preloading the hero image because of the presence of a srcset attribute…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* AMP Responsive Hero Image Preload Link Opt-In plugin bootstrap. | |
* | |
* @package Google\AMP_Responsive_Hero_Image_Link_Opt_In | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2021 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Responsive Hero Image Preload Link Opt-In | |
* Plugin URI: https://gist.github.com/westonruter/6adb64e6e8c858a40ac1dc51b03f16d8 | |
* Description: Enable preload links for responsive hero images which have <code>srcset</code> and <code>sizes</code> attributes. This is disabled by default because only Chrome supports <code>imagesrcset</code> and <code>imagesizes</code> on preload links. This eliminates the Optimizer from raising this notice: “CannotPreloadImage: Not preloading the hero image because of the presence of a <code>srcset</code> attribute, which can currently only be preloaded by Chromium-based browsers (see <a href="https://web.dev/preload-responsive-images/">https://web.dev/preload-responsive-images/</a>).” | |
* Version: 0.1 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Gist Plugin URI: https://gist.github.com/westonruter/6adb64e6e8c858a40ac1dc51b03f16d8 | |
*/ | |
namespace Google\AMP_Responsive_Hero_Image_Link_Opt_In; | |
use AmpProject\Optimizer\Transformer\PreloadHeroImage; | |
use AmpProject\Optimizer\Configuration\PreloadHeroImageConfiguration; | |
add_filter( | |
'amp_optimizer_config', | |
function ( $config ) { | |
$config[ PreloadHeroImage::class ][ PreloadHeroImageConfiguration::PRELOAD_SRCSET ] = true; | |
return $config; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c