#WordPress教程 – 三招解决调用谷歌字体加载过慢问题
因国内某些因素,导致大陆连接到Google字体的API:fonts.googleapis.com和themes.googleusercontent.com非常不稳定(N多谷歌IP被国内禁止访问),而Wordpress 3.8版本以后的后台、自带模版都调用了Google字体的CSS链接,因CSS在网页头部,所以导致网页加载速度非常非常慢。不过没关系,伤不起,我们还是躲得起的,下面提供三招回避这个问题。
第一:非插件插入代码解决:
把下面代码插入主题中的functions.php中:
代码1:
- // Remove Open Sans that WP adds from frontend
- if(!function_exists(‘remove_wp_open_sans’)):
- function remove_wp_open_sans(){
- wp_deregister_style(‘open-sans’);
- wp_register_style(‘open-sans’,false);
- }
- // 前台删除Google字体CSS
- //add_action(‘wp_enqueue_scripts’, ‘remove_wp_open_sans’);
- // 后台删除Google字体CSS
- add_action(‘admin_enqueue_scripts’,‘remove_wp_open_sans’);
- endif;
代码2
- 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’);
2种代码选一种即可解决访问后台慢的问题了!
第二:使用插件解决:
解决方法很简单,进入后台插件页面搜索Disable Google Fonts
或者Remove Open Sans font Link from WP core
其中之一安装启用即可。
第三:使用墙内公共库加载Google字体
具体方法:
打开wordpress代码中的文件wp-includes/script-loader.php文件,搜索:fonts.googleapis.com 找到这行代码:
$open_sans_font_url =
“//fonts.googleapis.com/css?family1=Open+Sans:300italic,400italic,600italic,300,400,600&subset=$subsets”;
把其中的 fonts.googleapis.com 替换为 fonts.useso.com 即可。
最好的办法
换一套不调用谷歌字体的主题。谷歌字体大多针对英文字体,如果是做中文博客,谷歌字体就没什么用了。绝大多数调用谷歌字体的通常多为国外的一些主题,而中文主题基本上不会用到谷歌字体。
黑桃Mark: