利用纯代码来禁用谷歌字体加速网站的响应速度-解决谷歌字体无法加载

WordPress 加载谷歌字体的代码在于 wordpress\wp-includes\script-loader.php 文件里面,这里展示三个代码.

wordpress解决后台打开慢代码屏蔽加载谷歌字体,首先我们想到的是为什么wordpress后台打开速度很慢,其实有俩个原因一个是谷歌加载字体和Gravatar头像被墙的原因带来的

// WordPress no longer uses or bundles Prototype or script.aculo.us. These are now pulled from an external source.
$scripts->add( 'prototype', 'https://ajax.lug.ustc.edu.cn/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1' );
$scripts->add( 'scriptaculous-root', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/scriptaculous.js', array( 'prototype' ), '1.9.0' );
$scripts->add( 'scriptaculous-builder', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/builder.js', array( 'scriptaculous-root' ), '1.9.0' );
$scripts->add( 'scriptaculous-dragdrop', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/dragdrop.js', array( 'scriptaculous-builder', 'scriptaculous-effects' ), '1.9.0' );
$scripts->add( 'scriptaculous-effects', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/effects.js', array( 'scriptaculous-root' ), '1.9.0' );
$scripts->add( 'scriptaculous-slider', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/slider.js', array( 'scriptaculous-effects' ), '1.9.0' );
$scripts->add( 'scriptaculous-sound', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/sound.js', array( 'scriptaculous-root' ), '1.9.0' );
$scripts->add( 'scriptaculous-controls', 'https://ajax.lug.ustc.edu.cn/ajax/libs/scriptaculous/1.9.0/controls.js', array( 'scriptaculous-root' ), '1.9.0' );
$scripts->add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );
// Hotlink Open Sans, for now
$open_sans_font_url = "https://fonts.lug.ustc.edu.cn/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=$subsets";
/*
 * Translators: Use this to specify the proper Google Font name and variants
 * to load that is supported by your language. Do not translate.
 * Set to 'off' to disable loading.
 */
$font_family = _x( 'Noto Serif:400,400i,700,700i', 'Google Font Name and Variants' );
if ( 'off' !== $font_family ) {
    $fonts_url = 'https://fonts.lug.ustc.edu.cn/css?family=' . urlencode( $font_family );
}

由于代码实在太多了,这里只截取了重要语句,具体大家想话可以直接在该文件下查找。

然后是禁用代码,直接将下面代码添加到主题的 functions.php 文件中即可。

function remove_open_sans_from_wp_core() {
    wp_deregister_style( 'open-sans' );
    wp_register_style( 'open-sans', false );
    wp_enqueue_style('open-sans',");
}
add_action( 'init', 'remove_open_sans_from_wp_core' );

第二就是解决掉Gravatar头像国内访问不了的问题

add_filter(‘get_avatar’, ‘wuzhuti_get_avatar’, 10, 3);
function wuzhuti_get_avatar($avatar) {
$avatar = str_replace(array(‘www.gravatar.com’, ‘0.gravatar.com’, ‘1.gravatar.com’, ‘2.gravatar.com’), ‘gravatar.duoshuo.com’, $avatar);
return $avatar;
}

把以上代码直接复制到functions.php模板当中就能解决上述问题,

分享到:

评论0

请先

限时活动48元全站资源随意下载
没有账号? 忘记密码?