欢迎光临
请一秒记住我们的网址:www.xinfangs.com !

WordPress免插件进行网页代码压缩优化网站速度

在WordPress中无需插件进行代码压缩和优化网站速度,可以通过以下方法实现:

教程

主题 全局配置文件 中,在functions.php文件中加入以下代码:
有些主题会是在functions-theme.php文件中:

<span class="hljs-comment">/*
WordPress免插件网页代码压缩
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">wp_compress_html</span><span class="hljs-params">()</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">wp_compress_html_main</span> <span class="hljs-params">($buffer)</span></span>{
        $initial=strlen($buffer);
        $buffer=explode(<span class="hljs-string">"<!--wp-compress-html-->"</span>, $buffer);
        $count=count ($buffer);
        <span class="hljs-keyword">for</span> ($i = <span class="hljs-number">0</span>; $i <= $count; $i++){
            <span class="hljs-keyword">if</span> (stristr($buffer[$i], <span class="hljs-string">'<!--wp-compress-html no compression-->'</span>)) {
                $buffer[$i]=(str_replace(<span class="hljs-string">"<!--wp-compress-html no compression-->"</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
            } <span class="hljs-keyword">else</span> {
                $buffer[$i]=(str_replace(<span class="hljs-string">"\t"</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
                $buffer[$i]=(str_replace(<span class="hljs-string">"\n\n"</span>, <span class="hljs-string">"\n"</span>, $buffer[$i]));
                $buffer[$i]=(str_replace(<span class="hljs-string">"\n"</span>, <span class="hljs-string">""</span>, $buffer[$i]));
                $buffer[$i]=(str_replace(<span class="hljs-string">"\r"</span>, <span class="hljs-string">""</span>, $buffer[$i]));
                <span class="hljs-keyword">while</span> (stristr($buffer[$i], <span class="hljs-string">'  '</span>)) {
                    $buffer[$i]=(str_replace(<span class="hljs-string">"  "</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*<span class="hljs-number">100</span>;   
        $savings=round($savings, <span class="hljs-number">2</span>);   
    <span class="hljs-keyword">return</span> $buffer_out;
}
ob_start(<span class="hljs-string">"wp_compress_html_main"</span>);
}
add_action(<span class="hljs-string">'get_header'</span>, <span class="hljs-string">'wp_compress_html'</span>);

然后我们刷新网页就OK了。那么如果我们要一些地方不要压缩,该如何做呢?

我们可以按照这个格式:

<span class="hljs-comment"><!--wp-compress-html--></span><span class="hljs-comment"><!--wp-compress-html no compression--></span>

不想压缩的地方

<span class="hljs-comment"><!--wp-compress-html no compression--></span><span class="hljs-comment"><!--wp-compress-html--></span>
赞(0) 加关注
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:WordPress免插件进行网页代码压缩优化网站速度
文章链接:https://www.xinfangs.com/1772.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

请一秒记住我们的网址:www.xinfangs.com !

去投稿去留言

扫码关注公众号

非常感谢,让我们一起创建更加美好的网络世界!

关注公众号

加微信

登录

找回密码

注册