关闭边栏(ESC)打开边栏(ESC)
现在好多站点都启用了代码的压缩,所以今天找了下资料,也来优化一下,本站采用的是张戈博客文章的方法,所以弄起来也挺方便。
部署功能代码
直接在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 |
//压缩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; } if ( !is_admin() ) { ob_start("wp_compress_html_main"); } } add_action('init', 'wp_compress_html'); |
Crayon Syntax Highlighter冲突解决
像本站一样,用了Crayon Syntax Highlighter代码高亮的话,在切换到文本的时候,代码也压一块了。
1 2 3 4 5 6 7 8 9 |
/* Crayon Syntax Highlighter高亮插件不启用压缩 */ function unCompress($content) { if(preg_match_all('/(crayon-|<\/pre>)/i', $content, $matches)) { $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", "unCompress"); |
最终压缩的效果如下:
请问评论框怎么可以做到什么都不填就发布?
你说的什么都不填写,是指内容吗?还是指填写网址之类
你文章的代码框是怎么实现的?
这只是一个插件:Crayon Syntax Highlighter