Last active
May 26, 2020 20:51
-
-
Save westonruter/98e2e6559389840111b430ab4db2075d to your computer and use it in GitHub Desktop.
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 Disable Default Gallery Shortcode Carousel plugin bootstrap. | |
* | |
* @package Google\AMP_Disable_Default_Gallery_Shortcode_Carousel | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2020 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Disable Default Gallery Shortcode Carousel | |
* Plugin URI: https://gist.github.com/westonruter/98e2e6559389840111b430ab4db2075d | |
* Description: Workaround for issue in the AMP plugin (<a href="https://github.com/ampproject/amp-wp/issues/4774">amp-wp#4774</a>) where galleries in Classic blocks (that is, <code>gallery</code> shortcodes) are unexpectedly displayed as carousels. This plugin forces them to be displayed the same as in the non-AMP version by default. | |
* 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/98e2e6559389840111b430ab4db2075d | |
*/ | |
namespace Google\AMP_Disable_Default_Gallery_Shortcode_Carousel; | |
/** | |
* Inject amp-carousel=false attribute into gallery shortcodes that don't already have an amp-carousel attribute. | |
* | |
* @param string $content Content. | |
* @return string Replaced content. | |
*/ | |
function inject_gallery_shortcode_attribute( $content ) { | |
return preg_replace_callback( | |
'/(?<=\[gallery\s)[^]]+(?=])/', | |
function ( $matches ) { | |
$atts = $matches[0]; | |
if ( false === strpos( $atts, 'amp-carousel=' ) ) { | |
$atts .= ' amp-carousel=false'; | |
} | |
return $atts; | |
}, | |
$content | |
); | |
} | |
add_filter( 'the_content', __NAMESPACE__ . '\inject_gallery_shortcode_attribute' ); |
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