Whenever you are looking for an awesome font for your next website, Google Fonts is usually the first choice. It provides a massive library of fonts free to use without having to worry about any cumbersome licenses or subscriptions. It is an open source library in which contributors can add fonts. In this article, we take a look at how to use Google Fonts in WordPress Themes and integrating them without using third-party plugins. In the end, we are going to see some tricks we can use to optimize Google Fonts integration into our website.
Why use Google Fonts in WordPress
The Google Fonts library is widely used across the internet for its extensive collection of fonts. One of the main reasons for its popularity is its usage policy. According to Google’s documentation, most of the Google Fonts are under the SIL Open Font License (OFL). This license permits usage and distribution even in commercial products. This policy makes it ideal for integration with WordPress, as WordPress is an Open Source platform. All WordPress themes and plugins are distributed under the GPL or an equivalent license.
Another reason Google Fonts are the best choice for WordPress websites is the sheer ease of using Google Fonts. Earlier, in order to integrate fonts into websites, web developers used to add the required files for fonts(TTF, OTF and more recently WOFF and WOFF2) and add them to CSS files with @font-face
rule. This method involved storing font files locally. Sometimes, these files would be bulky and contribute to increasing the loading times of a website. Google Fonts are completely server hosted. All you need to do is make an API request to Google’s server and receive the required font files as a response. This makes it very easy to integrate Google Fonts without the need to store the font files locally. Additionally, Google automatically optimizes these files and delivers the files in WOFF2 format.
For comparision, I downloaded both TTF and WOFF2 font files for Roboto regular font to see the difference in file sizes. Here are the screenshots comparing their sizes.
![]() |
![]() |
As you can see, WOFF2 files are significantly smaller than their TTF counterparts. Serving Google Fonts has a big advantage in terms of optimization and efficiency. We can further improve on it which we are going to discuss later. This makes Google Fonts a cleat choice for Web Developers when it comes to adding fonts to their websites.
How to Use Google Fonts in WordPress Themes
After discussing why Google Fonts are an obvious choice as website fonts, let’s see how to use Google Fonts in WordPress Themes. While there are a lot of plugins that can be used to add Google Fonts in WordPress, we’ll be focusing on doing it without plugins. For the sake of this article, I’m going to add the font Playwrite Cuba in my project. You can add any font of your choice. Steps are the same.
Getting the Embed Code
In order to add Google Fonts in our WordPress project, the first we need is the API request URL. Follow the following steps-
- Go to fonts.google.com.
- After searching for the font, open it. You’ll se a
Get font
button appear at the top-right of your screen.
- After clicking, our font will be added to the basket. You’ll notice the basket icons gets updated and a new screen appears.
- Click on
Get Embed Code
. A new screen appears with different options for getting the embed code.
- Notice that Playwrite Cuba a variable font. We can install the variable font (Full Axis) or a single weight (One Value). For simplicity sake, I’m using “One Value” and download the Regular 400 weight. Since we need the code for a website project, we select the “Web” tab in the right box. Notice the Embed code –
123<link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Playwrite+CU&display=swap" rel="stylesheet">
This is our embed code. We need to add it in our WordPress website. There are a few ways we can add it in our WordPress website. Let’s go over some of the ones I’ve been using or have used at some point of time.
Adding the embed code in a WordPress Theme
1. Adding the code in header.php
The first method is probably the most straight forward and the least recommended one. Add the code directly in the header.php file. If you are using a classic WordPress Theme, you should find it directly in the theme directory. In case you’re new to WordPress and unsure where to find the theme directory, in your WordPress installation, go to “wp-content” folder. There you’ll fin the “themes” folder. You can find your theme directory in the “themes” folder. It should be a directory with your theme name.
Open the header.php file. In the file, find the </head> closing head tag. Paste the embed code just before the closing head tag and save the file. Now, go to the browser and refresh your site. The site is now loading Google Fonts. In order to verify this, you can use the following steps–
- While on the site, press Cmd+U/Ctrl+U.
- The source code for the site should open in a new tab. Search for “fonts.googleapis.com”.
- This is the URL the browser makes an API request to in order to fetch fonts. You should be able to find the URL and the request made to Google servers.
2. Using the wp_head hook
In this method, we use the wp_head action hook to add Google Fonts to our theme. This method is an improvement from the first method as using this method, you don’t need to modify the header.php file. We just add a code snippet in the theme’s functions.php file. Open your theme’s functions.php file and add the following code and the end of the file–
1 2 3 4 5 6 7 8 |
function my_theme_add_google_fonts() { ?> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> <?php } add_action('wp_head', 'my_theme_add_google_fonts'); |
After sacing the file, you should be able to load Google Fonts in your WordPress website.
3. Using a Child Theme
In this method, we add Google Fonts using a Child Theme. This method has an added advantage that it will make our change future-proof. In case the theme gets updated in future, our changes will remain and not get reset after update. It is similar to the previous method but in this case, we add the code snippet in our Child Theme’s PHP file. In case you are not sure how to create a Child Theme, please refer to WordPress’ official documentation. It’s a comprehensive documentation and should help you create a Child Theme for your website.
In order to add Google Fonts, perform the following steps–
- Create a Child Theme of your theme and activate it.
- In the Child Theme’s
functions.php file, add the following code–
12345678function my_theme_add_google_fonts() {?><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"><?php}add_action('wp_head', 'my_theme_add_google_fonts'); - You should be able to load Google Fonts in your WordPress theme.
So, these were the basic ways you can add Google Fonts to your WordPress Theme or website. For most, it is enough and should get the job done. But what if I told you there are some further optimizations you could do in order to achieve further optimization. We’ll take a look at some of the ways we can optimize integrating Google Fonts into our WordPress Theme.
Optimizing the API Request
While the above methods are completely okay and work perfectly fine, they are not quite optimized. We can further optimize it. For that, we need to look a bit closer to the actual URL in the API request–
1 |
https://fonts.googleapis.com/css2?family=Roboto&display=swap |
In this URL, we are making a request to Google to retrieve the “Roboto” font with just the “Regular” font weight. We can request more weights by modifying the URL and adding the additional weights to the URL query like so–
1 |
https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;1,300;1,400&display=swap |
In this request, we are requesting a Regular, Regular Italic and a Light font weight. To get your required set of weights and styles, you can customize your query in fonts.google.com when you are getting your embed code. For the sake of this article, I’m just requesting the “Regular” font weight.
After entering the URL, the server sends us a CSS response that should look something like this–
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/* cyrillic-ext */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F; } /* cyrillic */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu5mxKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; } /* greek-ext */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu7mxKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+1F00-1FFF; } /* greek */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu4WxKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+0370-0377, U+037A-037F, U+0384-038A, U+038C, U+038E-03A1, U+03A3-03FF; } /* vietnamese */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu7WxKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB; } /* latin-ext */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu7GxKKTU1Kvnz.woff2) format('woff2'); unicode-range: U+0100-02AF, U+0304, U+0308, U+0329, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF; } /* latin */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; } |
It is a bunch of @font-face rules for different subsets of the font (latin, latin-ext, vietnamese, greek, cyrillic and more). This means the browser is loading all of these subsets. We can optimize this by loading just the subsets we want. If you know your website will only ever be using English, then we need to load only the latin subset. For this, we need to optimize our query. In case we need only Latin subset, we a subset argument to the query with value latin like so–
https://fonts.googleapis.com/css?family=Roboto&subset=latin&display=swap
While the request appears to be the same, Google has optimized the response in order to provide only the latin subset. Moreover, the API also detects the text and sends the subsets accordingly further optimizing the response.
Loading WOFF2 files locally
Another possible and in my opinion, the best optimization is loading the font files locally. More recently, users have started hosting fonts locally instead of requesting from a CDN. There are a couple of reasons for this transition-
- Firstly, it is a compliance measure. In order to comply with regulations such as the GDPR, resources are required to be loaded locally instead of a third-party service.
- Also, hosting resources locally reduces website latency and leads to better optimization.
- Also, to take benefit of the WordPress offering – Block Themes, it is preferred to host the files locally.
We can get the WOFF2 files from the API response we received earlier for our API request. Notice the src property for each of the @font-face rules. It provides link to the WOFF2 file for each subset. We can download the file from the link and bundle it in our theme and load locally. I usually prefer to store all WOFF2 files in assets/fonts folder in the theme directory.
So, we go ahead and download the WOFF2 file for the “Latin” subset and store it in assets/fonts folder naming the file “Roboto-Regular.woff2”. In order to load the font, we just need to add the @font-face rule in the required CSS file. Suppose we have our main.css file in assets/css folder, our @font-face rule should be something like this–
1 2 3 4 5 6 7 |
@font-face { font-family: "Roboto", sans-serif; font-weight: 400; src: url("../fonts/Roboto-Regular.woff2") format(woff2); font-stretch: normal; font-display: swap; } |
Loading fonts locally reduces a request to a third-party server and helps in optimizing the website.
We can get creative and futher optimize the request but I think this is enough for this article. In this article, we covered a lot – from embedding a simple code in our header.php to really going into the details of our API request, I hope you got to learn something from this article. In this artcle, we took a look at how to use Google Fonts in WordPress Themes without adding any third-party plugins. Looking forward to hearing your response. If you would like to gain more knowledge about the nitty gritty of WordPress, do check out our other articles. See you in the next article.