Categories
WordPress

Loading a Google Font in WordPress

function load_fonts() {
	wp_enqueue_style('googleFonts', 'http://fonts.googleapis.com/css?family=Montserrat:400,700');
}
add_action('wp_enqueue_scripts', 'load_fonts');

The proper way to load an external font library, such as from Google Fonts, is to use the wp_enqueue_scripts hook.

You can use the above sample code in your functions.php code or load it up via a custom plugin.

Breaking it down

Couple of key points:

  • wp_enqueue_script” is the hook
  • load_fonts” is your own custom function. You can call it whatever you want, as long as it’s not the same as any other function name
  • googleFonts” is a unique identifier for the style you’re loading. Make sure this is unique.

You can use this snippet to load an external resource, or a local one.

Using wp_enqueue_style makes the CSS available for aggregation and compression for better performance and load times.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.