Support » Plugin: AMP » Custom Element Scripts being removed

  • Resolved Nick the Geek

    (@nick_thegeek)


    Hi,

    After an update to the AMP WP plugin custom element scripts are being stripped.

    The code we are using to output the custom element scripts looks like this

    
    add_action( 'amp_post_template_head', 'theme_slug_amp_post_template_head' );
    /**
     * Adds the async script tags with custom element for amp analytics and potentially other scripts.
     */
    function theme_slug_amp_post_template_head() {
    	// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
    	?>
    	<!-- This is loaded from the theme 1 -->
    	<script async src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js" custom-element="amp-analytics"></script>
    	<script async src="https://cdn.ampproject.org/v0/amp-form-0.1.js" custom-element="amp-form"></script>
    	<?php
    	// phpcs:enable
    }
    

    I added the HTML comment to verify the hook was still working as expected.

    This theme is based on TwentySeventeen. Due to the way it was built, it doesn’t look right using Transitional or Standard modes so it is using Reader mode.

    There is a form and an analytics tag output into the AMP page. In some cases the amp-analytics script is left but the amp-form script is always stripped.

    Things I’ve tried:

    – Output an HTML comment to make sure the action is working as expected.
    – Change the priority to PHP_MAX_INT
    – Search Google and the git repo for any hint about this

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    There is actually no need to manually add these scripts to the page. The AMP plugin adds them automatically when a tag on the page requires them. If a script is not required, it is automatically removed. Son if you don’t have a form on the page, the amp-form script is automatically removed.

    Does this clear things up?

    Thread Starter Nick the Geek

    (@nick_thegeek)

    Thanks for the reply. It makes sense and I think is a good solution for a lot of implementations.

    The problem is the amp-form script is not being added automatically either. The page definitely has the amp-form inserted. It might be that it is inserted *after* the check to see if it is there. Is there a way to bypass the nanny logic and let us do it the way we were? It was validating and working and not it is not.

    Plugin Author Weston Ruter

    (@westonruter)

    Can you share the URL for where this can be seen? If this is the case, then it’s definitely a bug which we need to fix.

    What form is being added to the page?

    Thread Starter Nick the Geek

    (@nick_thegeek)

    Sorry, I have an NDA with this client. Can’t say who they are. 🙁

    I did check the HTML just now. I was assuming <amp-form> was present, but it seems that is being stripped out. It is 100% in the code injecting the form though.

    
    <amp-form>
    	<form
    		method="GET"
    		id="a_string<?php echo esc_attr( $count ); ?>"
    		class="a_string"
    		action="https://example.com"
    		target="_blank"
    	>
    <!-- Inputs -->
    	</form>
    </amp-form>
    

    This is added as part of a filter on

    
    add_filter( 'the_content', 'theme_string_insert_amp_form', 20 );
    

    So a filter is stripping the <amp-form> element and the custom element script.

    Plugin Author Weston Ruter

    (@westonruter)

    <amp-form> is not a valid AMP tag. It’s a special case here, but here the amp-form component is specifically for the <form> tag.

    Please refer refer to the amp-form docs: https://amp.dev/documentation/components/amp-form/

    So you can just remove the <amp-form> tag here. It’s getting removed due to being invalid AMP.

    Thread Starter Nick the Geek

    (@nick_thegeek)

    @westonruter ah, that appears to be the issue.

    I removed the tag and the script is being injected now. I’m assuming that when the tag is stripped something in the dom navigation is failing to identify that there is a valid <form> tag so the amp-form custom element script does not get inserted.

    This is working for me correctly now that I’ve removed the invalid script. It’s a bit confusing in the doc on this as usually the custom elements are indicating specific wrappers that have to be applied.

    Not sure if y’all want to see why the cleaned up and corrected code was not triggering the custom element script.

    Plugin Author Weston Ruter

    (@westonruter)

    I’ve been able to reproduce that issue, so this is a bug.

    Filed issue: https://github.com/ampproject/amp-wp/issues/4511

    Here’s a PR with a failing test showing the problem: https://github.com/ampproject/amp-wp/pull/4512

    We’ll get this fixed soon.

    Thank you for reporting.

    Plugin Author Weston Ruter

    (@westonruter)

    @nick_thegeek Here’s a 1.5.2-RC1 pre-release build with the fix for you to test: https://github.com/ampproject/amp-wp/issues/4511#issuecomment-608620174

    Plugin Author Weston Ruter

    (@westonruter)

    We’ve just published the 1.5.2 release to WordPress.org: https://wordpress.org/plugins/amp/

    You can update now via the WordPress admin.

    Release notes: https://github.com/ampproject/amp-wp/releases/tag/1.5.2

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Element Scripts being removed’ is closed to new replies.