强制替换Wordpress前台和后台谷歌字体webfont.js,ajax.googleapis.com,fonts.googleapis.com

74 阅读技术资料
0

发现Wordpress后台编辑器加载了ajax.googleapis.com,导致速度非常慢,用以下方法禁用它:(该代码可以禁用Wordpress前台和后台加载谷歌字体,适用于最新的WP)

/*
 * 通过Link标签的src地址判断后反注册禁用谷歌字体加载.
 */
if (!function_exists('disable_google_fonts')) {
    function drgf_dequeueu_fonts()
    {
        global $wp_styles;
 
        if (!($wp_styles instanceof WP_Styles)) {
            return;
        }
 
        $allowed = apply_filters('drgf_exceptions', ['olympus-google-fonts']);
 
        foreach ($wp_styles->registered as $style) {
            $handle = $style->handle;
            $src = $style->src;
            $exist = strpos($src, 'fonts.googleapis');
            if (false !== $exist) {
                if (!array_key_exists($handle, array_flip($allowed))) {
                    wp_dequeue_style($handle);
                    wp_deregister_style($handle);
                    wp_register_style($handle, '');
                }
            }
        }
    }
    add_action('wp_enqueue_scripts', 'drgf_dequeueu_fonts', 999);
    add_action('admin_enqueue_scripts', 'drgf_dequeueu_fonts', 999);
    add_action('login_enqueue_scripts', 'drgf_dequeueu_fonts', 999);
 
    /*
     * 禁用 Elementor 插件加载的谷歌字体.
     */
    add_filter('elementor/frontend/print_google_fonts', '__return_false');
}

没有效果的话,可以试试下面的:

function hc_cdn_callback($buffer) {
    return str_replace('cdn.staticfile.org/webfont/1.5.10/webfontloader.js', 'cdn.staticfile.org/webfont/1.5.10/webfontloader.js', $buffer); //主要这里替换一下
}
function hc_buffer_start() {
    ob_start("hc_cdn_callback");
}
function izt_buffer_end() {
    ob_end_flush();
}
add_action('init', 'hc_buffer_start');
add_action('shutdown', 'hc_buffer_end');

 


分享到:
发表于

Login

Welcome! Login in to your account

Remember meLost your password?

Don't have account. Register

Lost Password

Register