php - 防止 HTML Tidy 弄乱元标记(架构标记)

标签 php wordpress html htmltidy

我遇到了 HTML Tidy(最新版本 -- https://html-tidy.org)的严重问题。

简而言之:HTML tidy 转换这些行 HTML 代码

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
    <span property="itemListElement" typeof="ListItem">
        <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
            <span property="name">Codes</span>
        </a>
        <meta property="position" content="1">
    </span>
</div>

进入这些代码行 -- 请仔细查看 META TAGS 放置

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
    <span property="itemListElement" typeof="ListItem">
        <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
            <span property="name">Codes</span>
        </a>
    </span>
    <meta property="position" content="1">
</div>

这会导致模式验证出现一些严重问题。您可以在此处查看代码:https://search.google.com/structured-data/testing-tool/u/0/

由于这个问题,客户端(URL:https://techswami.in)的面包屑导航在搜索结果中不可见。

我在美化什么?

我的客户希望我让他/她的网站源代码看起来“干净、可读和整洁”。

所以我正在使用这些代码行让它为他/她工作。

注意:此代码在以下 WordPress 设置上 100% 完美运行。

  • 带有 FastCGI 缓存/MariaDB 的 Nginx
  • PHP7
  • Ubuntu 18.04.1
  • 最新的 WordPress 并与所有缓存插件兼容。

代码:

if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
    $tidy = new Tidy();
    $options = array('indent' => true, 'markup' => true, 'indent-spaces' => 2, 'tab-size' => 8, 'wrap' => 180, 'wrap-sections' => true, 'output-html' => true, 'hide-comments' => true, 'tidy-mark' => false);
    $tidy->parseString("$buffer", $options);
    $tidy->cleanRepair();
    $buffer = $tidy;
    return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');

我需要你们提供什么帮助?

你能告诉我如何防止 HTML Tidy 弄乱 META 标签吗?我需要参数。

谢谢。

最佳答案

<meta> 标签只能在父元素中使用:<head> , <meta charset> , <meta http-equiv> 此外,没有 property <meta> 中的属性元素。

这些很可能是 HTML-Tidy 的原因正在清理标记。

来源

关于php - 防止 HTML Tidy 弄乱元标记(架构标记),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51944802/

相关文章:

php - laravel 5.1同时注册、更新用户信息和发送数据到两个表

php - 如何配置 Lumen 通过 Pusher 广播事件?

php - 创建 wordpress 插件 ( PHPexcel )

javascript - 使用按钮更改图像(也是图像)-Javascript

html - 背景图像不显示在 html css 中

php - Symfony 2 + Doctrine 2 覆盖实体配置

在 WordPress 中调试 cron 任务

php - 从 Woocommerce 中的 URL 变量填充结帐字段值

html - 以不同宽度呈现的一行中的 Flex 元素

php - 从多个表中选择内容并显示