WordPress插件 WP Fastest Cache 也有标题所说的功能,开启压缩HTML代码功能。
今天追格小编介绍不用插件即可实现页面压缩功能,一起来看看吧。
//压缩 html 代码
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模板functions.php中添加以下代码即可。
-
WordPress函数:wp_trash_post 移动文章到回收站WordPress函数:wp_trash_post 移动文章到回收站
-
WordPress函数:wp_update_post 更新修改文章WordPress函数:wp_update_post 更新修改文章
-
WordPress函数:wp_trash_post_comments 移动文章评论到垃圾站WordPress函数:wp_trash_post_comments 移动文章评论到垃圾站
-
WordPress函数:wp_delete_attachment 删除附件WordPress函数:wp_delete_attachment 删除评论
-
WordPress函数:wp_update_user 更新用户信息WordPress函数:wp_update_user 更新用户信息
-
WordPress插件:音频播放器插件AudioIgniter Music PlayerAudioIgniter Music Player是一款WordPress音频播放器插件,在WordPress官网显示有超过10000+的下载量,UI界面看着还是不错的,有兴趣的同学可以看看。
暂无评论,抢个沙发...