Wordpress函数:使用str_replace去除头部的CSS


wordpress function: using str_replace to strip css in head

我目前正试图删除/评论wordpress的头部部分的标记。

这是我的函数。

function strip_style() { ?>
    <?php
    $commented_style = str_replace("<link rel="stylesheet" type="text/css" href="http://localhost/wp-content/themes/headway-208/style.css" />", "<!-- <link rel="stylesheet" type="text/css" href="http://localhost/wp-content/themes/headway-208/style.css" /> -->", $rawstring);
    echo commented_style;
    ?>
<?php
}
add_action('wp_head', 'strip_style', 1);

我也不能使用wp_enqueue_stylewp_deregister_style,因为我试图删除的行是硬编码的父主题。

基本上我想禁用默认的css (styles.css)的加载,所以我可以使用我自己的样式,而不是覆盖默认的样式。

请建议如果我使用str_replace()的方法是好还是不好。你能建议一个替代方案吗?

谢谢!

只需将header.php文件复制到您的子主题中,并编辑掉您不想出现的内容。如果在子主题和父主题中都有header.php,则使用子主题中的header.php。这对所有文件都有效。如果你想编辑任何父主题文件,只需将其复制到子主题并在那里编辑。

就css而言,首先是父主题style.css,然后是子主题style.css,所以子主题中具有相同特异性的规则将覆盖父主题规则,不需要任何繁琐的魔法。

也许我没有完全理解,但是为什么不复制现有的主题文件夹,重新命名它,并通过擦除style.css来创建自己的主题,然后重新开始呢?

在任何情况下,如果你必须执行str_replace(),你的代码都是错误的。首先,你的PHP标签到处都是,你不能嵌套它们。其次,您的字符串将无法正确解析,因为您在双引号字符串中使用了双引号。固定和简化:

<?php
function strip_style() {
    $link = '<link rel="stylesheet" type="text/css" href="http://localhost/wp-content/themes/headway-208/style.css" />';
    echo str_replace($link, '<!-- ' . $link . '-->', $rawstring);
    // But where does $rawstring come from?
}
?>

.

<?php
    add_action('wp_head', 'strip_style', 1);
?>