X

Use google fonts, with wp_register_style & wp_enqueue_style

This is just a simple snippet that uses wp_register_style & wp_enqueue_style to include Google fonts. Many people might want to include fonts by inserting html into wp_head when this method is by far the better way. In this sample I gave each font a unique handle based on the font name so we can easily see whats registered and enqueued. You will of course need to change the url for the font you would like to use within your theme. Once done just add this snippet to the functions.php of your WordPress theme and you are good to go.


Code Snippet:


function wps_enqueue_fonts() {
if (!is_admin()) {
wp_register_style('font_roboto', 'https://fonts.googleapis.com/css?family=Roboto:400,300,500,700', array(), null, 'all');
wp_register_style('font_oswald', 'https://fonts.googleapis.com/css?family=Oswald', array(), null, 'all');

wp_enqueue_style('font_roboto');
wp_enqueue_style('font_oswald');
}
}
add_action('wp_enqueue_scripts', 'wps_enqueue_fonts');

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!