How to Load Google Fonts in a Genesis Website

The latest design trends point directly towards the use of Google Fonts within a website.

Google believes that everyone should be able to bring quality typography to their web pages and applications, so they made it extremely easy to implement their fonts.

A few months ago I feel deeply in love with Typekit Fonts, but I’m still a huge fan of using Google Fonts — especially within sites running the Genesis Framework.

Using Google Fonts in a Genesis-Powered Website

Over a year ago we started implementing Google Fonts into our themes over at StudioPress.

We choose to use Google Fonts over other font sources such as Typekit and Cufon primarily because they were free — which allowed our users to easily use them without having to pay.

After all, we felt that showcasing an elegant look on a demo site but requiring folks to purchase a font would be an incredibly bad user experience.

So we made it easy for them by doing the dirty work and implementing them into our themes.

A New Method for Loading Google Fonts

Admittedly the way we began loading Google Fonts in our themes wasn’t the most ideal.

However, we recently began implementing them in a way that is much more efficient — and ultimately we feel is the proper way to do it.

Let’s take a look at how they are currently being loaded. If you open up one of our child theme’s style.css file, you’ll notice something like this towards the top of the style sheet.

/* Import Fonts
------------------------------------------------------------ */

@import url(http://fonts.googleapis.com/css?family=Merriweather);
@import url(http://fonts.googleapis.com/css?family=Open+Sans);

Using Yahoo’s page performance tool, you’ll see that this method requires (2) additional HTTP requests, and loads the Google Font style sheets separately. Here’s how the test report looks:

Yahoo Page Speed

While the file size for each style sheet isn’t very big, using an alternative method shouldn’t be overlooked. It’s true that on your site you might not experience any type of issues from this method, but high traffic sites such as Mashable could.

So I’d like to introduce the new method we are using, and I’ll show you why this is better. Open up one of our child theme’s functions.php file and place the following code:

/** Load Google fonts */
add_action( 'wp_enqueue_scripts', 'custom_load_google_fonts' );
function custom_load_google_fonts() {
	wp_enqueue_style( 'google-fonts', 'http://fonts.googleapis.com/css?family=Merriweather|Open+Sans', array(), PARENT_THEME_VERSION );
}

This code will load a Google Fonts style sheet and include both the Merriweather font as well as the Open Sans font.

Using Yahoo’s page performance tool, you’ll see that this method requires (1) additional HTTP request, and loads the Google Font style sheets combined. Here’s how the test report looks:

Yahoo Page Speed

UPDATED: Combining Fonts in Your Style Sheet

It’s been brought up in the comments below about the possibility of combining the Google Fonts at the top of a style sheet, which would serve the same purpose as the method above.

So simply doing this would also save HTTP requests and help with page speed:

/* Import Fonts
------------------------------------------------------------ */

@import url(http://fonts.googleapis.com/css?family=Merriweather|Open+Sans);

With HTTP Requests, Less is More

Imagine if you had 3 (or more) Google Fonts being loaded onto your site? If you do the math, and multiply the additional HTTP requests by the number of page views per day, you’d quickly see why this is important.

To make things clearer, let’s use StudioPress as an example. On any given day, we see anywhere between 50,000-100,000 page views — which means if we loaded three Google Fonts on each page, we’d see an additional 100,000-200,000 HTTP requests per day.

Thankfully we’ve got some pretty smart people and a really good server setup, which is optimized for high volumes of traffic across our sites.

If you want to experience the enhanced performance and security that comes from the synthesis of WordPress-optimized servers, take a look at our Synthesis Hosting plans.

A solid server and reduced HTTP requests via this method of loading Google Fonts should result in a much better user experience!

Email Newsletter

Like what you read here in this blog post?
Get more like it delivered to your inbox daily.

Comments

  1. says

    Not sure what kind of difference it makes with YSlow, but I don’t put Google Fonts into my CSS, I put them in the wp_head Hook section of Simple Hooks. It makes your “Avoid CSS @import” in page speed a much better grade.

    • says

      Putting the link in the WP Head hook is another way to do it if you’re using Simple Hooks. That should work just fine, just make sure you combine the fonts in the code if you’re using more than one, so you don’t end up with the same issue.

  2. says

    Excellent post and it come out at the right time because I was facing the problem of google font not working right on my site.

    I had a question Brian! Does it matter how many google fonts you are importing to the one child theme? How many google fonts we can import into one child theme to good effect?

  3. says

    Ryan,
    Such an informative post! I always just did it through the style.css only, and even though it had an increased server load, it never bothered me. I now know what you mean by adding too many HTTP requests. Any way this could be done through simple hooks as well?

  4. says

    Getting old!. Really am! Eye sight gone at 45 hahaha. Just wanted to send you a message and say thanks for the great tutorials and that i’m a big fan of Tri’s and the people that train for them.

    9.8 times out of ten i love you guys at Copyblogger media!

    Recently discovered your community themes which i think adds a different style of child themes to your range from designers that love building on the Genesis framework.

  5. says

    Awesome tip. I have been looking for ways to spice up the old Revolution Pro Media 1.0.
    Not quite sure how to add this in at first since I don’t have Genesis theme, but Google’s own site was very helpful with tips on how to do that. http://www.google.com/webfonts. Perhaps Terence, you can take a look there.

  6. says

    I was just working with page load times for a client last week – and now here’s a great solution. It’s amazing how timely you are. :) I’ll be bookmarking this one.

  7. says

    Ahh finally ! I could not get a new font I was trying to use on a site I am designing to show up! Was driving me nuts! Using the code in the functions.php did the trick! THANK YOU!!! :) Seems the old @import method I was doing, is not working as simply as it used to a few months ago.

  8. says

    I found another important use of importing Google fonts through functions.php (as suggested above). I was having a problem on my blog – using the Fabric Child Theme – when using WP Minify plugin, as the @import of the fonts from Google in style.css was not being handled properly during ‘minification’ by the plugin. As a result, the blog header and post titles were unable to produce the intended custom fonts. Once I shifted the import to functions.php and deleted it from style.css, everything worked out well.

    Incidentally, I was referred to this article by the very helpful Jennifer Baumann of Copyblogger Media Support. Thanks Jennifer!!

      • says

        What does “there’s a chance we have a Google Font function in Genesis” mean? Confused if you meant “[we'll] have a GF function in Genesis [soon]” or “we [already] have a…”, since I assume you of all people would know if you did or didn’t ;)

        Searching the new support site and forums didn’t bring anything useful up for me (I find the new support site extremely difficult to find anything on — I miss the PHPBB forums — so it doesn’t surprise me too much).

  9. says

    Actually @import prevents the loading of the rest of the css file until the import is complete (by blocking them). Though minimal impact, it is actually faster to have the extra HTTP request than to use @import. So the better and preferred method would be to use wp_enqueue_scripts hook though @import may be easier. See here for more information.

    • says

      Thanks Travis. I remembered reading Steve Souders post a while ago it’s part of what prompted the question. If I read correctly I think the @import blocking depends on the browser, but the info is all so dated it’s hard to know. YMMV, but I don’t really care if it only blocks on IE8 if everything else runs parallel which I think is the case.

      Even if using wp_enqueue_scripts I think you’d want to combine multiple font requests into a single http request.

      Google has their own font loader though and I thought it invoked some other magic to optimize loading (vague recollection of a google blog video I saw more than a year ago).

  10. says

    I know this is a late reply to this post, but I was doing 3 @import fonts in my style sheet and just combined them into one. Sweetness. Thank you Brian!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>