Change the default font of your theme in WordPress to google fonts.
Environment
WordPress 5.5.1 Theme: Sparkling (I have child-theme sparkling-child)
What is Google Fonts?
Google font is abundant font set that can be used in websites without install and upload. It can be used by just putting code in your HTML.
We believe the best way to bring personality and performance to websites and products is through great design and technology. Our goal is to make that process simple, by offering an intuitive and robust collection of open source designer web fonts.
To understand What child themes in WordPress is and create it
Environment
WordPress 5.5.1 Theme: Sparkling
What is Theme and child-theme in WordPress
What is theme?
WordPress Themes can provide much more control over the visual presentation of your content and other data on your WordPress site, as well as behavior of certain site’s elements while interacting with visitors.
And you can customize the themes from Appearance>Customize.
What are themes made of?
The following is an example of file construction of the theme. You can see these files from Appearance>Theme Editor.
Required files
index.php: the main template file
style.css
Additional files
PHP files
template files such as header.php, page.php, sidebar.php and 404.php
Localization files: this is used for translating an internationalized theme
CSS files
bootstrap.css, flexslider.css, etc
Graphics
JavaScript
customizer.js, functions.js, flexslider-custom.js, etc.
Text files
license.txt, readme.txt, etc.
What is child-theme?
As indicated in the overview, a child theme inherits the look and feel of the parent theme and all of its functions, but can be used to make modifications to any part of the theme. In this way, customizations are kept separate from the parent theme’s files.
Create a new directory in wp/wp-content/themes and named it “<theme name>-child”. In my case, the name is sparkling-child.
2. Create a stylesheet: style.css
Create style.css in <theme name>-child directory. And write information at the top of style.css as below. “Theme URI” is the path to the child theme in the server and “Template” is the directory name of the parent theme. “Theme Name” needs to be unique.
/*
Theme Name: Sparkling Child
Theme URI: http://example.com/sparkling-child/
Description: Sparkling Child Theme
Author: Nako
Author URI: http://example.com
Template: sparkling
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: blog
Text Domain: sparklingchild
*/
/* Add your css here*/
3. Enqueue stylesheet
Create functions.php in <theme name>-child directory. And write the function to enqueue stylesheets of the parent theme.
If the parent theme has some css in addition to ‘/style.css’, you should contain them in theme_enque_styles(). If the stylesheet of the child theme doesn’t loaded automatically, you should enqueue ‘child-style’ in theme_enque_styles().
4. Install and customize child theme
Open Appearance>Themes in Dashboard.
Then select the child-theme you created and customize it.
5. Activate child theme
Appendix
The order of style sheet to read will changed if child-theme is added. So you should move some additional css from the parent style.css to child style.css.
When only parent theme is used, sparkling/style.css is loaded after loading font (Google Fonts). You can use the font in parent sparkling/style.css.
When child theme is used in addition to parent theme, sparkling/style.css is loaded at first and font (Google Font) is load after that. You can’t use the font in parent sparkling/style.css.
So you should put css related to the font in sparkling-child/style.css or read every stylesheet in sparkling-child/function.php.