Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove or dequeue google fonts in wordpress twentyfifteen

Tags:

wordpress

I have created a child theme of twenty fifteen. I'd like to remove the google fonts loaded in wp_head but I can't get it to work. What is loaded is:

<link rel='stylesheet' id='twentyfifteen-fonts-css'  href='//fonts.googleapis.com/css?family=Noto+Sans%3A400italic%2C700italic%2C400%2C700%7CNoto+Serif%3A400italic%2C700italic%2C400%2C700%7CInconsolata%3A400%2C700&#038;subset=latin%2Clatin-ext' type='text/css' media='all' />

I've created a function.php in my child theme but I can't figure out how to remove this. I've gotten other things remove using:

remove_action('wp_head', '...');

But I can't figure out how to remove the fonts.

Also, any tips on how to remove the IE condition statements and css would be very helpful!

Thanks!

like image 905
Peachy Avatar asked Mar 18 '15 22:03

Peachy


3 Answers

TwentyFifteen uses a custom function to build a Google fonts URL which is then used with wp_enqueue_style(). To remove Google fonts create a function in your child theme to dequeue the stylesheet.

Use the wp_enqueue_scripts hook and make sure to give it a higher priority than the hook in the parent theme. The default is 10 so in my example I use 20.

Example:

function wpse_dequeue_google_fonts() {
    wp_dequeue_style( 'twentyfifteen-fonts' );
}
add_action( 'wp_enqueue_scripts', 'wpse_dequeue_google_fonts', 20 );
like image 170
Nathan Dawson Avatar answered Jan 03 '23 14:01

Nathan Dawson


Deregister/Dequeue styles is best practice

https://codex.wordpress.org/Function_Reference/wp_deregister_style https://codex.wordpress.org/Function_Reference/wp_dequeue_style

But you can use 'style_loader_src' filter too, to filter out styles with string condition, or other conditions, here is example for google fonts

add_filter( 'style_loader_src', function($href){
if(strpos($href, "//fonts.googleapis.com/") === false) {
return $href;
}
return false;
});
like image 45
PayteR Avatar answered Jan 03 '23 13:01

PayteR


Open theme's functions.php and find a function called twentyfifteen_fonts_url() - it handles all the fonts stuff. In default file it starts on line 144. Edit it to your needs.

Other options:

  1. Use a plugin to control fonts - https://wordpress.org/plugins/typecase/
  2. Use a plugin to remove default fonts - https://wordpress.org/plugins/remove-open-sans-font-from-wp-core/
  3. Use wp_deregister_style() function to manually unregister that stylesheet. See here.

As for the IE conditional - check the next function in functions.php, called twentyfifteen_scripts(). It starts on line 196.

like image 41
Ihor Vorotnov Avatar answered Jan 03 '23 14:01

Ihor Vorotnov