由于我想避免使用W3C验证警告,我目前在我的函数文件中使用以下代码段从排队的脚本和样式表中删除type属性:
// Remove type attribute from scripts and stylesheets
add_filter('style_loader_tag', 'remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'remove_type_attr', 10, 2);
function remove_type_attr($tag, $handle) {
return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}
但这不适用于插件加载的文件。有谁知道这个问题的解决方案?
答案 0 :(得分:0)
您可以通过简单的调整来捕获WordPress的整个输出内容:https://stackoverflow.com/a/22818089/5556440
从上面提供的问题中查看@ kfriend的答案。
<!-- wp-content/mu-plugins/buffer.php -->
<?php
/**
* Output Buffering
*
* Buffers the entire WP process, capturing the final output for manipulation.
*/
ob_start();
add_action('shutdown', function() {
$final = '';
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
// that buffer's output into the final output.
$levels = ob_get_level();
for ($i = 0; $i < $levels; $i++) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
echo apply_filters('final_output', $final);
}, 0);
然后在新final_output
过滤器内,执行preg_replace:
<!-- wp-content/themes/theme-name/functions.php -->
add_filter('final_out', 'remove_type_attr', 10, 2);
function remove_type_attr($content) {
return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $content);
}