WordPress无需插件进行网页代码压缩优化网站速度-代码版

作者:晓白pos网 发布时间:2020-12-26 17:48:49

wordpress seo优化不仅仅是为了优化百度关键词排名,简单的说,如果你的网站很轻便,浏览速度很快,那你的网站访问量就很可观,也就是说用户体验良好,那就需要进行wordpress各方面的优化

使用方法

在你主题 全局配置文件 中

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

/*
WordPress免插件网页代码压缩
*/
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);
    return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');

然后我们刷新网页就OK了。那么如果我们要一些地方不要压缩,该如何做呢?
我们可以按照这个格式:

<!--wp-compress-html no compression-->不想压缩的内容<!--wp-compress-html-->

你也可以将此代码整理成段代码的形式。如果你是vieu主题,可以在主题目录的/include/modules/mo_editbutton.php文件</script>之前添加一行

QTags.addButton( '不压缩代码', '不压缩代码', '<!--wp-compress-html-->', "<!--wp-compress-html no compression-->" );

之后便可以在老版编辑器直接使用了

标签:,

联系邮箱:ert5tyx@163.com(三个工作日内处理)
Copyright © 2020 · 晓白pos网 皖ICP备2022013394号-7