首页 > 解决方案 > 如何在 wp_head() 中删除没有类或 id 的内联 css

问题描述

 <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style media="all">
Bounce of code
</style>
<link rel='stylesheet' id='...' href='...' media='all'/>
<script>...</script>
...
</head>

我想删除<style media="all"> Bounce of code </style>头部的一部分。它在 wp_head() 中。有没有办法做到这一点?

提前致谢。

标签: phpwordpresswordpress-theming

解决方案


除了调试整个网站及其主题/插件之外,另一种解决方案是简单地将其添加到主题的末尾functions.php

function start_wp_head_buffer()
{
    ob_start();
}

function end_wp_head_buffer()
{
    $in = ob_get_clean();
    $in = preg_replace('/' . preg_quote('<style media="all">') . '\s*' . preg_quote('Bounce of code') . '\s*' . preg_quote('</style>', '/') . '/s', '', $in);

    echo $in;
}

add_action('wp_head', 'start_wp_head_buffer', 0);
add_action('wp_head', 'end_wp_head_buffer', PHP_INT_MAX);

推荐阅读