压缩HTML页面的好处就是略微提升页面加载速度,并给那些爱扒皮的缺德玩意制造些许麻烦。好了将如下代码添加在functions.php中即可:
- //压缩WordPress前端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');
- //当检测到文章内容中有代码标签时文章内容不会被压缩
- 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");
本文地址:https://www.13qing.com/87.html
温馨提示:文章内容系作者个人观点,不代表 墨情博客对观点赞同或支持。
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
温馨提示:文章内容系作者个人观点,不代表 墨情博客对观点赞同或支持。
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!