Believe you can and you’re halfway there.
相信自己,你也就成功了一半
压缩前端HTML页面就是删除页面所有空行和制表符等不必要的内容,从而实现加速 WordPress 的效果,小羿早前给大家发过插件「Autoptimize」可以实现压缩HTML页面,下面给大家分享代码版的:
「后台」→「外观」→「编辑」 「functions.php」文件,把以下的内容添加进去:
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("<a href="https://www.521cd.cn/tag/wp" title="更多关于 wp 的文章-彩豆博客" target="_blank">wp</a>_compress_html_main");}add_action('get_header', '<a target="_blank" href="https://www.521cd.cn/tag/wp" title="View all posts in wp">wp</a>_compress_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("<a href="https://www.521cd.cn/tag/wp" title="更多关于 wp 的文章-彩豆博客" target="_blank">wp</a>_compress_html_main"); } add_action('get_header', '<a target="_blank" href="https://www.521cd.cn/tag/wp" title="View all posts in wp">wp</a>_compress_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');
不压缩部分的相应代码改为:
<!--wp-compress-html--><!--wp-compress-html no compression-->此处代码不会被压缩,主要是避免压缩带来的错误,比如JS错误<!--wp-compress-html no compression--><!--wp-compress-html--><!--wp-compress-html--><!--wp-compress-html no compression--> 此处代码不会被压缩,主要是避免压缩带来的错误,比如JS错误 <!--wp-compress-html no compression--><!--wp-compress-html--><!--wp-compress-html--><!--wp-compress-html no compression--> 此处代码不会被压缩,主要是避免压缩带来的错误,比如JS错误 <!--wp-compress-html no compression--><!--wp-compress-html-->
感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
暂无评论内容