Support » Plugin: AMP » Gallery sanitizer duplicated in featured image and post content

  • Resolved Will Woodward


    As recommended previously, I’m adding a gallery programatically using a sanitizer (see below for a very simplified example!)

    Since the update (0.6.2 -> 0.7) this has started injecting the gallery below the featured image as well as in the content.

    After some investigation I realised this is because the amp_content_sanitizers filter is now being called/used in the featured image as well as the post content, via the amp_get_content_sanitizers helper function (see includes/templates/class-amp-post-template.php, lines 320 and 358.)

    Could you please advise how I should proceed?

    add_filter('amp_content_sanitizers', 'addGallery', 10, 2);
    function addGallery($sanitizer_classes, $post)
        $sanitizer_classes['AmpGalleryInjectionSanitizer'] = [
            'post' => $post,
        return $sanitizer_classes;
    class AmpGalleryInjectionSanitizer extends AMP_Base_Sanitizer
        public function sanitize()
            $carousel = AMP_DOM_Utils::create_node($this->dom, 'amp-carousel', [
                'type' => 'slides',
            // Carousel children omitted from this example for simplicity
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Ryan Kienstra


    Possible Alternative

    Hi @willjw,
    Thanks, that’s a good question.

    If I understand your use case right, filtering the_content might be a better approach.

    add_filter( 'the_content', function( $content ) {
            if ( is_amp_endpoint() ) {
                    $content .= '<your-gallery-markup>'; // Example markup
            return $content;
    } );

    Sanitizers here work best when they operate on existing markup. For example, they can identify an <img> and transform it to an <amp-img>.

    If I understand your use-case, I think a filter might be better.

    And is_amp_endpoint() will ensure this only alters the content on your AMP pages.

    Plugin Contributor Ryan Kienstra



    The conditional in my snippet above should probably also check that function_exists( 'is_amp_endpoint' ), in case this plugin is ever deactivated:

    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {

    Thanks Ryan, that’s a much better solution than using a sanitizer!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Gallery sanitizer duplicated in featured image and post content’ is closed to new replies.