
wordpress压缩前端网页代码
把下面的代码添加到主题functions.php中
即可压缩网页,删除多余的空格,使网页更加紧凑,
减速网页传输。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//前端压缩代码开始
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("\n\n", "\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);
$buffer_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";
return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');
//前端压缩代码结束
💘 相关文章
- 三条命令实现WordPress丝滑顺畅的压缩迁移方案
- wordpress A TimThumb error has occured的解决方法
- 在OpenMediaVault5.0系统下搭建Wordpress的坑
- wordpress刪除需要FTP賬戶和密碼解決方法
- 解决WordPress经典编辑器空白可视化功能不可用的方法
- Openlitespeed wordpress伪静态出现404错误解决方法
- 一款专注于wordpress开发的本地套件工具-Local
- Nginx使用Memcached缓存加速wordpress,秒开网站
- wordpress一些優化代碼,實則可用,收藏版
- wordpress删除wp-json和REST API