Home > php教程 > php手册 > WordPress优化方法汇总

WordPress优化方法汇总

WBOY
Release: 2016-06-06 20:13:16
Original
1537 people have browsed it

发现wordpress慢是一个很大的问题,特开一博来总结优化的相关方法,介于我也是在优化中更新的,此文会长期更新。 一、优化博客中插件加载的js位置。 很多插件在header中注册了js脚本,但希望脚本放在footer部分加载,如果修改插件可能会影响插件的升级,下面

      发现wordpress慢是一个很大的问题,特开一博来总结优化的相关方法,介于我也是在优化中更新的,此文会长期更新。

一、优化博客中插件加载的js位置。

      很多插件在header中注册了js脚本,但希望脚本放在footer部分加载,如果修改插件可能会影响插件的升级,下面的方法来自于stackoverflow : How to put my javascript in the footer
【方法】把下面的代码添加到 主题中的functions.php中

remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
Copy after login

N、其它技巧

1、博客中投放了百度广告,延迟异步加载的方法:现在header中加一个全局变量

var onLoadCalls = [];
Copy after login

再在小工具中或其它部分

View Code JAVASCRIPT

onLoadCalls.push(function(){
    loadScript("http://cpro.baidu.com/cpro/ui/c.js",function(){
         BAIDU_CLB_DUP2_fillSlotAsync('u432452', 'sidebar-ad'); //sidebar-ad为广告展示的div#id
    });
});
Copy after login

接下来在尾部的js代码中添加如下代码

function loadScript(src, callback){
    var doc = document,
        body = doc.body,
        //创建一个新script来加载
        script = doc.createElement('script');
 
    script.type = 'text/javascript';
    script.src = src;
    script.onerror = script.onload = script.onreadystatechange = function(e){
        e = e || window.event;
        if(!script.readyState || /loaded|complete/.test(script.readyState) || e === 'error'){
            callback(src);
            script.onerror = script.onload = script.onreadystatechange = null;
        }
    };
    body.appendChild(script);
}
 
$(document).ready(function(){
    if(onLoadCalls && onLoadCalls.length){
        $.each(onLoadCalls, function(index, fn){
            fn.call(null, $);
        });
    }
});
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template