Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active October 20, 2021 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnregan3/e18ae44e9139d0de7426681d3caed785 to your computer and use it in GitHub Desktop.
Save johnregan3/e18ae44e9139d0de7426681d3caed785 to your computer and use it in GitHub Desktop.
Strip out all Shortcodes for a WordPress AMP template.
<?php
/**
* Strip out all shortcode content.
*
* This is a quick and dirty way
* to ensure no shortcodes introduce
* invalid markup into an amp template.
*
* @param string $content WP Post content.
*
* @return string
*/
function jr3_amp_strip_all_shortcodes( $content ) {
global $shortcode_tags;
if ( ! amp_is_request() || empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
return $content;
}
$regex = get_shortcode_regex();
$content = preg_replace( "/$regex/", '', $content );
return $content;
}
add_filter( 'the_content', 'jr3_amp_strip_all_shortcodes' );
@DevWael
Copy link

DevWael commented Jul 27, 2021

Hi there,

  • this function is_amp_endpoint() is deprecated, use amp_is_request() instead.
  • this line preg_replace( "/$regex/", '', $content ); needs to be replaced with $content = preg_replace( "/$regex/", '', $content );.

Regards...

@johnregan3
Copy link
Author

Updated. Thanks @DevWael !

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