这几天一直在想办法如何不用插件用纯代码压缩网站,像往常一样到比较关注的博客去找找,其中 张戈博客 的甚是好用,值得推荐。你可以点击超链接去学习,也可以在这里直接获取果实。
复制以下代码到当前使用主题的 functions.php 文件中即可。
/* 自动在存在高亮代码的文章收尾插入免压缩注释 By 张戈博客 */ 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'); function Code_Box($content) { $matches = array(); /* 一下是匹配高亮代码的关键词,本代码适用于 Crayon Syntax Highlighter 插件,其他插件请自行分析关键词即可 */ $c = "/(crayon-|<\/pre>)/i"; if(preg_match_all($c, $content, $matches) && is_single()) { $content = '<!--wp-compress-html--><!--wp-compress-html no compression-->'.$content; $content.= '<!--wp-compress-html no compression--><!--wp-compress-html-->'; } return $content; } add_filter( "the_content", "Code_Box" );
但是,他这种代码不会压缩有代码演示的页面,如果你想更彻底的连有代码演示的页面也压缩,你也可以直接使用我优化后的代码。但首先保证的是:你的高亮代码是以 <pre> 开头,以 </pre> 结尾。如果是,那么你可以大胆的复制以下代码到当前使用主题的 functions.php 文件中。
/* 功能描述: 压缩 html 功能介绍页面: https://www.ivicos.com/57.html */ function compress_html() { function 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("compress_html_main"); } add_action('get_header', 'compress_html'); function pre_prettyprint_begin($content) { /* <pre> 前添加 包裹头代码 */ global $post; $content = preg_replace('/<pre(.*?)>/', "<!--wp-compress-html--><!--wp-compress-html no compression--><pre>", $content); return $content; } add_filter ('the_content', 'pre_prettyprint_begin'); function pre_prettyprint_end($content) { /* </pre> 后添加 包裹尾代码 */ global $post; $content = preg_replace('/<\/pre>/', "</pre><!--wp-compress-html no compression--><!--wp-compress-html-->", $content); return $content; } add_filter ('the_content', 'pre_prettyprint_end');
好了,Wordpress 代码压缩 html 的方法就分享到这里,非常感谢你的来访。如果你喜欢本站,请不要忘记收藏本站,以便下次继续访问;也可以 关注站长微博 随时获取最新动态。你的支持就是我最大的动力!
转载请注明:爱维科斯 » WordPress 代码压缩 html
如果文章对你有帮助,欢迎点击上方按钮打赏作者