Troubleshooting Common AMP Plugin Issues: From Invalid Scripts to Custom Code
Content
Working with the AMP plugin for WordPress can sometimes lead to unexpected validation errors and compatibility issues. Based on common community support threads, this guide addresses the most frequent problems and provides clear, actionable solutions.
Why Do These AMP Errors Occur?
The core principle of AMP is to create fast, secure, and streamlined mobile pages. To achieve this, AMP enforces strict content restrictions. The most common source of conflicts is the prohibition of most custom JavaScript, which is a standard AMP requirement, not a bug in the plugin itself. The AMP plugin sanitizes content to meet these standards, often removing or modifying non-compliant code.
Frequent Issues and Their Solutions
1. Invalid Script Errors with Code Snippet Plugins
Problem: Plugins like WPCode Lite that add custom code to your header or footer often inject JavaScript. This triggers validation errors such as "Invalid inline script" or "Invalid script: admin-bar.js".
Solution: Since AMP pages do not allow custom JavaScript, you must find alternative methods to add functionality:
- Use AMP-compatible components to recreate the desired feature.
- For analytics or other scripts, use the dedicated
amp-analyticscomponent. - If the code is not essential for the AMP version, use PHP conditionals like
if ( ! is_amp_endpoint() ) { ... }to prevent it from loading on AMP pages.
2. Adding Custom HTML or Ads to AMP Templates
Problem: Users often struggle to insert custom HTML, text banners, or ad codes into the header or other parts of their AMP template.
Solution: The method depends on your AMP mode (Transitional or Legacy Reader):
- Transitional Mode: Your active theme's templates and stylesheets are used. Modify your theme's files directly or use the WordPress Customizer. To hide an element only on AMP pages, use the
is_amp_endpoint()function in your PHP or add a specific CSS body class for AMP. - Legacy Reader Mode: Create a template override. Copy the template file (e.g.,
header-bar.php) fromwp-content/plugins/amp/templates/towp-content/themes/your-child-theme/amp/and modify the copied file. - Use the
amp_post_template_headorwp_headaction hooks to inject content. - For ads, consider using the
amp-auto-adscomponent or manually placingamp-adcomponents in your template overrides.
3. AMP Components Not Working as Expected
Problem: Components like amp-script or amp-recaptcha-input may not function or may cause validation errors.
Solution:
- amp-script Limitations:
amp-scriptruns in a web worker and has no access to the main DOM. This means certain actions, likewindow.locationredirection, will not work. Alternative AMP components or state management (amp-bind) must be used instead. - Component Nesting Rules: AMP has strict rules about where components can be placed. For example, an
amp-recaptcha-inputmust be a descendant of aformtag and cannot be placed inside anamp-mega-menu. Carefully review the official AMP documentation for each component's requirements. - Content Sanitization: The WordPress editor (TinyMCE) may strip out valid AMP component tags like
<amp-carousel>. To avoid this, use a custom HTML block or a shortcode to output the required code.
4. CSS and Styling Issues
Problem: CSS styles might appear to be missing, especially when using amp-bind to toggle classes dynamically.
Solution: The AMP optimizer performs "tree shaking" to remove unused CSS. If a class is hidden by default (e.g., display: none) and then revealed later via amp-bind, the optimizer might remove it. A workaround is to use the :not() selector to hide elements without setting them to display: none initially, preventing the style from being considered unused.
Key Takeaways
- AMP is JavaScript-Light: The most common conflicts arise from trying to use custom JavaScript. Seek out AMP-specific components for functionality.
- Validation is Key: Use the AMP Validator (often available in your browser's Developer Console when on an AMP page) to identify the exact source of errors.
- Theme Compatibility Matters: Some themes may handle AMP templates better than others. If you encounter persistent issues in Transitional mode, reaching out to your theme's support for guidance is recommended.
- Consult the Documentation: The AMP project website is the definitive resource for understanding component behavior and specifications.
By understanding the rules of the AMP ecosystem and using the correct methods to add customizations, you can resolve most common issues and ensure your pages are both valid and functional.
Related Support Threads Support
-
Header ad AMP legacy themehttps://wordpress.org/support/topic/header-ad-amp-legacy-theme/
-
Simple javascript using amp-script not workinghttps://wordpress.org/support/topic/simple-javascript-using-amp-script-not-working/
-
How I can add some HTML code under AMP headerhttps://wordpress.org/support/topic/how-i-can-add-some-html-code-under-amp-header/
-
Question on AMPhttps://wordpress.org/support/topic/question-on-amp/
-
Override HTML Tagshttps://wordpress.org/support/topic/override-html-tags/
-
Jetpack Functionshttps://wordpress.org/support/topic/jetpack-functions/
-
Hook to add contenthttps://wordpress.org/support/topic/hook-to-add-content/
-
Adding blocking behavior not workinghttps://wordpress.org/support/topic/adding-blocking-behavior-not-working/
-
AMP Carousel html being removed by post editorhttps://wordpress.org/support/topic/amp-carousel-html-being-removed-by-post-editor/
-
How do i insert a scripthttps://wordpress.org/support/topic/how-do-i-insert-a-script/
-
Optimize AMPhttps://wordpress.org/support/topic/optimize-amp/
-
Youtube Videohttps://wordpress.org/support/topic/youtube-video-31/
-
Using a different header.php for AMP (header_amp.php)https://wordpress.org/support/topic/using-a-different-header-php-for-amp-header_amp-php/
-
Hide element in AMP “Transitional” modehttps://wordpress.org/support/topic/hide-element-in-amp-transitional-mode/
-
AMP Header Edithttps://wordpress.org/support/topic/amp-header-edit-2/
-
Need AMP-Javascripthttps://wordpress.org/support/topic/need-amp-javascript/
-
amp-recaptcha-input component in amp-mega-menuhttps://wordpress.org/support/topic/amp-recaptcha-input-component-in-amp-mega-menu/
-
Any Hook amp status enable or disible programellyhttps://wordpress.org/support/topic/any-hook-amp-status-enable-or-disible-programelly/
-
AMP Script being moved from intohttps://wordpress.org/support/topic/amp-script-being-moved-from-into/
-
iframe script in AMPhttps://wordpress.org/support/topic/iframe-script-in-amp/
-
Just Adding Code To Amp Pageshttps://wordpress.org/support/topic/just-adding-code-to-amp-pages/
-
Redirect after Delayhttps://wordpress.org/support/topic/redirect-after-delay/
-
I want to use jquery and script AMP pagehttps://wordpress.org/support/topic/i-want-to-use-jquery-and-script-amp-page/
-
Element Problem in Single Post AMP If Add Youtube Iframehttps://wordpress.org/support/topic/element-problem-in-single-post-amp-if-add-youtube-iframe/
-
AMP next pagehttps://wordpress.org/support/topic/amp-next-page-2/
-
Sanitizer stripping amp-subscriptions encrypted ciphertexthttps://wordpress.org/support/topic/sanitizer-stripping-amp-subscriptions-encrypted-ciphertext/
-
add nonce to amp-scriptshttps://wordpress.org/support/topic/add-nonce-to-amp-scripts/
-
Title Tag Position in Headhttps://wordpress.org/support/topic/title-tag-position-in-head/
-
How to add custom css for amp pages?https://wordpress.org/support/topic/how-to-add-custom-css-for-amp-pages/
-
Amp tree shaking is removing class styles which is used later using amp-bindhttps://wordpress.org/support/topic/amp-tree-shaking-is-removing-class-styles-which-is-used-later-using-amp-bind-2/
-
How to add custom [ads] code in Transitional mode?https://wordpress.org/support/topic/how-to-add-custom-ads-code-in-transitional-mode/
-
AMP not working with WPCode Litehttps://wordpress.org/support/topic/amp-not-working-with-wpcode-lite/
-
Sanitizer for amp-mega-menuhttps://wordpress.org/support/topic/sanitizer-for-amp-mega-menu/
-
Content in header: Hiding on page load?https://wordpress.org/support/topic/content-in-header-hiding-on-page-load/
-
Amp Google Play App Bannerhttps://wordpress.org/support/topic/amp-google-play-app-banner/