WordPress 前端 html 网页代码压缩优化(插件版和代码版)

2016-04-06 06:15 2,413 7 条评论 龙笑天下
Dragon主题购买

为了优化网站的访问速度,对网页进行压缩是非常不错的一条方法。尽管说压缩 wordpress 页面后对查看源代码来说不太友好,更可以说是惨目忍睹。但是压缩页面的好处就是减少了页面的体积,从访问速度上来说,更快些,尽管这些是肉眼看不到的,但是这样做确实很好,另一方面就是给扒皮者制造麻烦,他们不得不重新整理页面代码的整洁性!

插件版

如果你正在找一款只可以压缩 wordpress 代码,优化 wordpress 的工具,不需要其他多余的功能,那么这个插件你应该会喜欢:WP-HTML-Compression。与同类 wordpress 优化插件Autoptimize有相似的功能,它的主要作用是——删除前端页面所有空行和制表符等不必要的内容、简化代码,从而实现加速 WordPress 的效果。你甚至不需要做任何设置,下载,安装,启用。不要小看页面中的空行,删去后可以节省大量载入时间,这也就是为什么 jQuery、Bootstrap 等文件要提供“Uncompressed”和“Compressed”版的原因。

当然,如果你有些代码不想被压缩,那么此插件也提供了非常人性化的注释方法:

<!--wp-html-compression no compression-->
此标签里的代码将受到保护,不会被插件压缩
<!--wp-html-compression no compression--> 

代码版

当然,如果不想安装插件的同学也可以试试下面代码!压缩效果可见本站。

直接在 functions.php 中加入下面的代码即可,来自@张戈

/**
* WordPress 前端 html 网页代码压缩优化(插件版和代码版)
* https://www.ilxtx.com/wordpress-html-compression.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--> 

比如,如果你跟我一样使用了 Crayon Syntax Highlighter 高亮插件,为了防止“代码切换到纯文本模式时,代码全挤在一团”,可在 function.php 中加入以下代码:

/* 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");

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2016-03-31

2016-04-25

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more