[Resolved] How to filter a script in AMP version of the site

Home Forums Support [Resolved] How to filter a script in AMP version of the site

Home Forums Support How to filter a script in AMP version of the site

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1055535
    Junaid

    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&#8217; async></script>

    Can you help me in this regard?

    If you want to understand the by your own then please check out this result.

    https://validator.ampproject.org/#url=https%3A%2F%2Finspiredn.com%2Ffunny-quotes-and-sayings%2Famp%2F

    #1055538
    Junaid

    Actually, the script is this one,

    <script type=’text/javascript’ src=’https://cdn.ampproject.org/v0/amp-ad-0.1.js&#8217; async custom-element=”amp-ad”></script>

    This script is appearing at line 39 and 41 in the validator.

    #1055619
    David
    Staff
    Customer Support

    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.

    #1055632
    Junaid

    I had removed the hooked element. I have tried to disable all other options than a single plugin.

    #1055648
    David
    Staff
    Customer Support

    Do you have any functions that could be requesting the file?

    #1055671
    Junaid

    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.

    #1055778
    David
    Staff
    Customer Support

    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

    #1055804
    Junaid

    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.

    #1055808
    David
    Staff
    Customer Support
    #1056051
    Junaid

    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?

    #1056461
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    In that case, I would use this plugin: https://wordpress.org/plugins/code-snippets/

    #1056650
    Junaid

    I don’t know how but the issue is now resolved by it’s own.

    Thank you for your support.

    #1057186
    Tom
    Lead Developer
    Lead Developer

    No problem 🙂

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.