Obviously it’s punishable by death to suggest placing Google Font links in head.php. I used to enqueue them as styles in scripts.php but now that I’ve added Grunt to my workflow everything is so beautifully minified and I don’t want to throw that off.
So, how is everyone adding Google Fonts to their Roots built sites?
I add it to head.php… I understand enqueuing scripts via Wordpress, but that is really so that Wordpress is aware of scripts and they are not loaded twice. I don’t see Google fonts as a Javascript or Jquery plugin, so I just paste in the code in the head.
So basically, you would probably continue to enqueue it in Wordpress if that works for you. But since you are loading it from Google and not your own server, you can’t really minify and concatenate it.
Something about adding files directly to head.php seems blasphemous.
Google Fonts provide an alternative using @import CSS syntax so what about using that somewhere? I realize that could be tricky because @import rules need to be the first lines of a CSS file.
Would love some other voices on this if anyone else has a recommendation…
Thanks @jayseventwo, guess I could’ve tried this on my own.
Also, if you don’t want to use @import rules you can just dive into the link and copy/paste the raw font-face rules from Google. That might be my newest preferred method. Only downside I can think of is if they ever change the path to the font files…
It’s true, working with Wordpress, if you are doing it correct, you enqueue your javascript and don’t load it manually.
However, going even a step further, is understanding what and why you do it. Then you understand when you can and possibly should do things differently.
In any case, there are multiple ways to get Google fonts on your site. @import is probably fine, as long as that’s the only .css file you’re actually importing. Speaking from experience, having multiple @import’s creates a noticeable lag on the website. That of course doesn’t affect importing less files, mind you, since they are all combined and minified.
/**
* Google Font
*/
function load_fonts() {
wp_register_style('googleFonts', 'http://fonts.googleapis.com/css?family=Neucha|Great+Vibes|Rock+Salt|Vast+
wp_enqueue_style( 'googleFonts');
}
add_action('wp_print_styles', 'load_fonts');
With multiple fonts, I would continue to use the link option. I’m not sure if I would bother with the action though, you can easily just add it to the head directly.
By flatten, I meant going to the import src and copying the CSS over. The CSS is different depending on the device, so you would be significantly limiting support.
I usually put them directly in the head myself. If I’m using an asynchronous font library, I manually stick the script tag+code in the head too in the hopes of minimizing the FOUC.
In a new project with Roots 7.0.0, I’m calling Google Fonts in _main.js via the script provided in Google Fonts (‘javascript’ tab in ‘Use’ section) and it works fine!
I actually prefer this method because I often run into issues where FOUT messes with things like Isotope, so it allows for events. More information can be seen here.