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

5.14K 浏览技术资料
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('ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.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');

 


分享到:
Qi 已回答的问题 2021年12月11日
添加评论
0

速度慢的话,检查一下可能是这些原因导致的。


分享到:
Qi 已回答的问题 2021年12月11日
添加评论
您正在查看4个答案中的1个,单击此处查看所有答案。
写下您的答案。

Login

Welcome! Login in to your account

Remember me Lost your password?

Don't have account. Register

Lost Password

Register