WordPress能够和JSDelivr很好的结合,WordPress后台打开慢,可以用JSDelivr CDN加速,前台可以使用JSDelivr CDN for WordPress插件加速访问。

在你的WordPress主题functions.php中加入:

/**
 * Enqueue scripts for all admin pages.
 * https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/.
 */
function wpmore_admin_script()
{
    $script =array('jquery','wp-polyfill','react','react-dom','utils','wplink','shortcode','underscore','clipboard','hoverIntent');

    foreach ($script as $value) {
      wp_deregister_script($value);
    }

    wp_register_script('jquery', 'https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js', array(), '1.12.4', true);

    wp_register_script('wp-polyfill', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/dist/vendor/wp-polyfill.min.js', array(), 'master', true);

    wp_register_script('react', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/dist/vendor/react.min.js', array(), 'master', true);

    wp_register_script('react-dom', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/dist/vendor/react-dom.min.js', array(), 'master', true);

    wp_register_script('utils', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/utils.min.js', array(), 'master', true);

    wp_register_script('wplink', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/wplink.min.js', array(), 'master', true);

    wp_register_script('shortcode', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/shortcode.min.js', array(), 'master', true);

    wp_register_script('underscore', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/underscore.min.js', array(), 'master', true);

    wp_register_script('clipboard', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/clipboard.min.js', array(), 'master', true);

    wp_register_script('hoverIntent', 'https://cdn.jsdelivr.net/gh/WordPress/WordPress@master/wp-includes/js/hoverIntent.min.js', array(), 'master', true);

    foreach ($script as $value) {
      wp_enqueue_script($value);
    }
}
add_action('admin_enqueue_scripts', 'wpmore_admin_script');
WordPress后台打开慢,用JSDelivr CDN加速
WordPress后台打开慢,用JSDelivr CDN加速

替换之后哦,加载JS都是毫秒级别的加载了。

WordPress后台加载慢?load-scripts.php、load-styles.php不合并JS、CSS

这里有几个问题需要注意:

为什么只加速了几个JS,而不是全部JS?

加速首先要找到 wp_deregister_script的名字,不太好找到的,我就没有全部去找了,大家可以自行去添加。

部分JS替换jsdelivr后,会出现函数未定义之类的错误,猜测是不应该跨域操作的JS。

是否可以使用jsdelivr加速CSS?

当然可以,这与上面替换JS一样,只不过需要用到 wp_deregister_stylewp_enqueue_style

文章版权归 更好的WordPress主题 所有,未经许可不得转载,责任编辑:admin。

分享到: