最近谷歌所有服务都不稳定或不能访问,包括字体服务(Google Fonts Open Sans),导致 WordPress 后台加载速度缓慢。如果 WordPress 主题使用了谷歌字体服务,也会因为访问不稳定载入延时,解决方法很简单:

将以下代码直接添加到主题的 functions.php 中即可禁用后台 Google Fonts

方法一:

//禁用Open Sans
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;

 

方法二:

//禁用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' );

 

 

方法三:替换 open sans 字体的加载源:

前台:使用360加速节点,将主题文件中出现的所有 fonts.googleapis.com 字样替换为 fonts.useso.com

后台:修改 wp-includes/script-loader.php 文件,把 fonts.googleapis.com 替换为 fonts.useso.com

 

方法四:

如果你对代码不熟悉,可安装插件 Disable Google Fonts 或 Remove Open Sans font Link from WP core ,启用即可。

 

附官方Twenty系列主题去除代码:

class Disable_Google_Fonts {
 public function __construct() {
 add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );
 add_action( 'after_setup_theme', array( $this, 'register_theme_fonts_disabler' ), 1 );
 }
 public function disable_open_sans( $translations, $text, $context, $domain ) {
 if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
 $translations = 'off';
 }
 return $translations;
 }
 public function disable_lato( $translations, $text, $context, $domain ) {
 if ( 'Lato font: on or off' == $context && 'on' == $text ) {
 $translations = 'off';
 }
 return $translations;
 }
 public function disable_source_sans_pro( $translations, $text, $context, $domain ) {
 if ( 'Source Sans Pro font: on or off' == $context && 'on' == $text ) {
 $translations = 'off';
 }
 return $translations;
 }
 public function disable_bitter( $translations, $text, $context, $domain ) {
 if ( 'Bitter font: on or off' == $context && 'on' == $text ) {
 $translations = 'off';
 }
 return $translations;
 }
 public function register_theme_fonts_disabler() {
 $template = get_template();
 switch ( $template ) {
 case 'twentyfourteen' :
 add_filter( 'gettext_with_context', array( $this, 'disable_lato' ), 888, 4 );
 break;
 case 'twentythirteen' :
 add_filter( 'gettext_with_context', array( $this, 'disable_source_sans_pro' ), 888, 4 );
 add_filter( 'gettext_with_context', array( $this, 'disable_bitter' ), 888, 4 );
 break;
 }
 }
}
$disable_google_fonts = new Disable_Google_Fonts;

# # #

分享此文: 微信 新浪微博 QQ
上一篇

什么才能打败时间?

下一篇

小事永远只是小事

发表评论