wordpress seo优化不仅仅是为了优化百度关键词排名,压缩网站代码提升网站速度有利于用户体验!但需要有一点基础的朋友使用,不然容易出现一些奇怪的问题!
主题 全局配置文件 中
在functions.php
文件中加入以下代码:
有些主题会是在functions-theme.php
文件中:
/*
WordPress免插件网页代码压缩juan920.com
*/
function wp_compress_html(){
function wp_compress_html_main ($buffer){
$initial=strlen($buffer);
$buffer=explode("<!--wp-compress-html-->", $buffer);
$count=count ($buffer);
for ($i = 0; $i <= $count; $i++){
if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
$buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
} else {
$buffer[$i]=(str_replace("t", " ", $buffer[$i]));
$buffer[$i]=(str_replace("nn", "n", $buffer[$i]));
$buffer[$i]=(str_replace("n", "", $buffer[$i]));
$buffer[$i]=(str_replace("r", "", $buffer[$i]));
while (stristr($buffer[$i], ' ')) {
$buffer[$i]=(str_replace(" ", " ", $buffer[$i]));
}
}
$buffer_out.=$buffer[$i];
}
$final=strlen($buffer_out);
$savings=($initial-$final)/$initial*100;
$savings=round($savings, 2);
return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');
然后我们刷新网页就OK了。那么如果我们要一些地方不要压缩,该如何做呢?
我们可以按照这个格式:
<!--wp-compress-html--><!--wp-compress-html no compression-->
不想压缩的地方
<!--wp-compress-html no compression--><!--wp-compress-html-->
如果我的文章对你有帮助,欢迎移至上方按钮打赏
© 版权声明
THE END