Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save westonruter/279bcb54336449ce55d8887fb0fcf5b9 to your computer and use it in GitHub Desktop.
Save westonruter/279bcb54336449ce55d8887fb0fcf5b9 to your computer and use it in GitHub Desktop.
AMP Disable ES Modules (unless enabled with query param)
<?php
/**
* AMP Disable ES Modules by Default plugin.
*
* @package AMP_Disable_ESM
* @author Weston Ruter
* @link https://gist.github.com/westonruter/279bcb54336449ce55d8887fb0fcf5b9
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: AMP Disable ES Modules by Default
* Plugin URI: https://gist.github.com/westonruter/279bcb54336449ce55d8887fb0fcf5b9
* Description: Disable the use of ES modules by the AMP Optimizer unless a <code>amp_optimizer_config</code> query parameter is provided to allow them.
* Version: 0.1
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: MIT
* Gist Plugin URI: https://gist.github.com/westonruter/279bcb54336449ce55d8887fb0fcf5b9
*/
use AmpProject\Optimizer\Transformer\RewriteAmpUrls;
use AmpProject\Optimizer\Configuration\RewriteAmpUrlsConfiguration;
if ( ! isset( $_GET['amp_esm_enabled'] ) ) {
add_filter(
'amp_optimizer_config',
static function ( $config ) {
$config[ RewriteAmpUrls::class ][ RewriteAmpUrlsConfiguration::ESM_MODULES_ENABLED ] = false;
return $config;
}
);
}
@westonruter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment