This will be a quick tutorial. Have you been getting amp script errors like:
- The tag ‘amp-ad extension .js script’ appears more than once in the document. This will soon be an error.
- The tag ‘amp-analytics extension .js script’ appears more than once in the document.
- 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.
Please Rate This Post:You’ll also like:
Please share this article to help others. Thanks