Archived topic
How to filter a script in AMP version of the site
12 replies · Started by Junaid on November 6, 2019
Hello there,
I just installed a plugin that helped me add my ads code to AMP version. But now there is an error with the page in the validator.
Here is the title of the error,
The tag 'amp-ad extension .js script' appears more than once in the document. This will soon be an error.
It means that a script is added twice.
Here is the script that I want to filter from the PHP,
<script type='text/javascript' src='https://cdn.ampproject.org/v0.js' async></script>
Can you help me in this regard?
If you want to understand the by your own then please check out this result.
Actually, the script is this one,
<script type='text/javascript' src='https://cdn.ampproject.org/v0/amp-ad-0.1.js' async custom-element="amp-ad"></script>
This script is appearing at line 39 and 41 in the validator.
Hi there,
unless you hooked in that code then its being added by a plugin. If the plugin adds the script then you can remove the one you did hook in.
I had removed the hooked element. I have tried to disable all other options than a single plugin.
Do you have any functions that could be requesting the file?
I have only AMP plugins. One is simple AMP plugin by Google and other is AMP incontent Ads by Github.
If you want to check dashboard. I don't mind it.
I can only assume that both of those plugins are making the same request.
I also see this a the very bottom of your website:
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; } );
Not sure what thats doing there, it looks like it has been added to a Hook. As its PHP it would need to be added to your child themes functions.php
Yes, I had found this code as a solution somewhere on internet and I added to the hook. Where sbould I place it so that it could work properly?
Secondly, both plugins are not for Ads. Only one is for Ads in AMP.
This article explains:
The idea is good, but I couldn't understand how can I use child theme. I found some information on Google but that's too much complicated to install a Child Theme. Isn't there any other way? Should I try adding directly to functions.php?
Hi there,
In that case, I would use this plugin: https://wordpress.org/plugins/code-snippets/
I don't know how but the issue is now resolved by it's own.
Thank you for your support.
No problem :)