wordpress:添加<noscript>标签使<head>标签自动关闭

Mat*_*yr' 10 html wordpress noscript amp-html accelerated-mobile-page

美好的一天.我使用WordPress的网站"AMP"发生器,这个插件的行动之一就是之前刚刚添加biolerplate标签</head>和<noscript>具有降效标签.谷歌搜索控制台不断警告我一个特定的错误:"样板的强制标签'noscript外壳'缺失或不正确."

所以我开始调查.我发现,"东西"是</head><body>在<noscript>开始标记之前注入,如果我改变了放置noscript标签的位置,其他两个也被移动了.

这是呈现的代码:

<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
</head><body><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
Run Code Online (Sandbox Code Playgroud)

我做了很多测试,似乎<noscript>标签是这个错误的重点.我还读了一些其他的答案,说文档不应该是严格的xhtml 1.1,事实并非如此,事实上这些是在页面上呈现的第一件事:

<!DOCTYPE html>
<html amp lang="it-IT"><head><meta charset="utf-8"><link rel="dns-prefetch" href="https://cdn.ampproject.org">
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚我可以做些什么来解决许多网站上存在的错误,包括不同的插件和主题(以及wordpress版本).

更新: 我试图<noscript>在代码中手动添加一个标签,每次我在<head>部分中添加它,部分都会关闭,<body>部分打开,没有任何类(所以它看起来像一个bug).如果你想看到这个bug,只需到这里查找代码:

https://www.assistenzamalasanita.com/2015/07/errori-medici-durante-il-parto-come-si-valutano/amp/

更新2 禁用所有插件并切换到默认主题对此没有影响.此外,我已经将整个站点复制到另一台服务器上,其中问题不存在,WP站点是相同的,并且serer配置也应该是,但是在工作的站点上我可以看到HTTP请求有一个关于php版本(7.0.2)的属性,其他网站没有.

这可能会影响页面的渲染吗?看到有效的网站:https: //www.doors.it/iride/2017/10/risarcimento-malasanita/amp

更新3 这是插件的一部分,实际上是编写样板的代码(头部和身体错误的标签).

add_action( 'amp_post_template_head', 'amp_post_template_add_boilerplate_css' );
function amp_post_template_add_boilerplate_css( $amp_template ) {
    ?>
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
    <?php

    }
Run Code Online (Sandbox Code Playgroud)

正如您可能注意到的那样,整个块都插入一个镜头,它背后没有逻辑,页面应该按原样呈现(在该代码中,我试图将标签更改为<nonscript>一切都很好,头部标签没有关闭,身体标签在正确的地方打开,并有正确的类.

GFa*_*rgo 1

正如评论所建议的和第三次编辑所说,这是由挂钩函数引起的。幸运的是,这通常是一个非常简单的修复!

就像评论中提到的 @sally-cj 一样,使用remove_action( 'amp_post_template_head', 'amp_post_template_add_boilerplate_css' );来完全阻止此函数的触发。

如果我们不想在管理中输入样式,您可以在主题代码中复制过滤器,而不使用有问题的</noscript>标签。

add_action( 'amp_post_template_head', 'so_52185596_custom_boilerplate_css' );
function so_52185596_custom_boilerplate_css( $amp_template ) {
?>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style>
<?php

}
Run Code Online (Sandbox Code Playgroud)