How To Fix AMP Script Appears More Than Once In The Document

AMP Logo

This will be a quick tutorial. Have you been getting amp script errors like:

  1. The tag ‘amp-ad extension .js script’ appears more than once in the document. This will soon be an error.
  2. The tag ‘amp-analytics extension .js script’ appears more than once in the document.
  3.  An AMP component ‘script’ tag is present more than once in the document.

For some reason, the official AMP plugin from time to time tends to auto inject scripts into AMP pages and that is what causes these errors. From what one of the plugin’s developer says, it looks like the plugin scans your content for amp components and automatically injects the corresponding script to make those components work.

To stop the auto injection, simply add the following code to your functions.php

add_filter( ‘amp_post_template_data’, function( $data ) {
$data[‘amp_component_scripts’] = array_merge(
$data[‘amp_component_scripts’],
array(
‘amp-iframe’ => ‘https://cdn.ampproject.org/v0/amp-iframe-latest.js’,
‘amp-analytics’ => ‘https://cdn.ampproject.org/v0/amp-analytics-latest.js’,
‘amp-sticky-ad’ => ‘https://cdn.ampproject.org/v0/amp-sticky-ad-latest.js’,
‘amp-form’ => ‘https://cdn.ampproject.org/v0/amp-form-latest.js’,
‘amp-ad’ => ‘https://cdn.ampproject.org/v0/amp-ad-latest.js’,
)
);
return $data;
} );

You can add as many component scripts as you need as long as the corresponding component is active on your AMP page.

Hope this works for you like it did for me. Let me know by leaving a comment.


Discover more from Patchwork Of Tips

Subscribe to get the latest posts sent to your email.

You’ll also like:

Please share this article to help others. Thanks