# 关闭google fonts补全

- Link: https://yjyj.net/learn/wordpress-learn/4722.html
- Published: 2014-09-03T23:46:14+08:00
- Author: 小 虾

⚡ 生猛大蝦编辑部 · 30 秒核心看点

 * 用 gettext_with_context 过滤器把 Open Sans 的开关文案改成 off，间接关闭字体加
   载。
 * 也可通过 wp_deregister_style 和 wp_register_style 处理 open-sans 样式，思路更
   直接。
 * 核心结论：别改核心文件，挂钩函数即可完成禁用，适合 WordPress 维护场景。

来北京出差快2周了.blog都长草了哈哈哈

    ```decode:true
    class Disable_Google_Fonts {
    public function __construct() {
    add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
    }
    public function disable_open_sans( $translations, $text, $context, $domain ) {
    if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
    $translations = 'off';
    }
    return $translations;
    }
    }
    $disable_google_fonts = new Disable_Google_Fonts;
    ```

 

    ```decode:true
    // remove_wp_open_sans
    if (!function_exists('remove_wp_open_sans')) {
        function remove_wp_open_sans() {
            wp_deregister_style( 'open-sans' );
            wp_register_style( 'open-sans', false );
        }
        // Uncomment below to remove from admin
        add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
    }
    function remove_open_sans() {
            wp_deregister_style( 'open-sans' );
            wp_register_style( 'open-sans', false );
            wp_enqueue_style('open-sans','');
    }
    add_action( 'init', 'remove_open_sans' );
    ```

 

❓ 延伸问答与排错指南（FAQ）

WordPress 里怎么关闭 Open Sans 字体？

可通过挂钩函数注销 open-sans 样式，再重新注册为空地址，避免字体继续被加载。若只想
影响后台，也可以把相关操作挂到 admin_enqueue_scripts 上，减少对前台的干扰。

为什么要禁用 Google Fonts 相关字体？

主要是为了减少外部字体请求，降低加载依赖和潜在延迟。对一些追求轻量、稳定或内网访问
的站点来说，这类优化能让页面更干净，后台也更省资源。

这类修改需要改 WordPress 核心文件吗？

不需要。更稳妥的做法是通过主题 functions.php 或自定义插件添加钩子函数来处理，升级
WordPress 时也不容易被覆盖，维护成本更低。

📚 【[WordPress 学习](https://yjyj.net/category/learn/wordpress-learn)】专栏延伸
阅读与热门精选（[全站文章归档](https://yjyj.net/archive)）：

 * [再撸wordpress后台菜单](https://yjyj.net/learn/wordpress-learn/4684.html)
 * [处理部分WordPress核心代码或功能，让你的网站更快](https://yjyj.net/learn/wordpress-learn/4669.html)
 * [WordPress 4.0 !](https://yjyj.net/learn/wordpress-learn/4724.html)
 * [wordpress不同分类显示不同的sidebar](https://yjyj.net/learn/wordpress-learn/4735.html)
 * 热门必读[程序员漫游指南](https://yjyj.net/what/5830.html)
 * 热门必读[升级一下iptables](https://yjyj.net/learn/centos/3208.html)

## Comments

### By: Caola

- Link: https://yjyj.net/learn/wordpress-learn/4722.html/comment-page-1#comment-12940
- Published: 2014-10-14T21:15:19+08:00

我还是喜欢用谷歌的字体库，所以自己反代了一下，虽然360的CDN库也有反代，但他不支持
https方式，所以我还自己反代了一下（fonts.cao.la）

### By: 小虾

- Link: https://yjyj.net/learn/wordpress-learn/4722.html/comment-page-1#comment-12952
- Published: 2014-10-15T21:56:08+08:00

给力! 这个必须支持啊\~
