Build Google Fonts API Compatible Font String
Most themes use Google Fonts. Most themes appear to copy and paste the same code to do so from some kind of a template theme that exists somewhere.
When theme developers do that, to me that is an indication that they really need a built-in class that does it for them. This does that.
Furthermore, it allows the webmaster to specify an alternate font server that uses the same API without needing to modify the theme code.
Fully Qualified Class Name:
This class is primarily intended for theme developers. It makes it easy to create a Google Fonts API compatible query string while at the same time allowing the blogmaster the ability to specify an alternate font server simply by defining a constant in their
The constructor for this this class does not take any parameters.
The class assumes that italic variants of specified weights are desired unless you tell it not to, solving a common problem where theme developers forget to specify they want that.
Usually only the
functions.php file in a theme needs to be changed for the theme to make use of this class.
public static function weightToNumeric($weight): int
This method is intended for internal use in the class, but since it is called by a another static method, it has to be static and thus public.
What it does, it takes a singe argument which can either by a numeric font weight or a string description of a font weight, and it returns the corresponding integer font weight.
public static function parseFontParameters(string $fontstring): array
Some themes kindly allow the blogmaster the option of defining their own font string for use with Google Fonts. This static function takes that string and parses it into components that are suitable for use when defining fonts to the class. It returns an array consisting of three values:
- The font family (aka typeset) name.
- An array containing integers corresponding to requested font weights.
- A boolean value specifying whether or not italic variants were requested.
public function setFont(string $family, array $weights = array(), bool $italic = true): void
This method is used to tell the class about a webfont you want to use. The first argument is the font family, the second is an array of weights that you want from that family, and the third argument is whether or not you want the italic variant.
public function setSubset(array $subsets): void
This method is used to specify the language sets you want used. The class already defaults to always include
My opinion is that there is no need to even bother defining the subsets. Google Fonts ignores the defined subsets for any browser that supports the
unicode-range: declaration in CSS, which is virtually every single one. However it is possible though unlikely that some Google API compatible font servers need that specified for non-Latin character support.
public static function gfontDnsPreload(): void
This method is intended for internal use, but that use requires that it have a public visibility. This method fixes a performance issue in the way most themes use Google Fonts.
With most themes,
fonts.googleapis.com is added to the web page (automatically by WordPress Core) but that is only where the CSS files that defines the web font is served from. The fonts themselves are served from
fonts.gstatic.com. This class adds that domain to the DNS prefetch so that as soon as the browser realizes it needs to download a font, the domain name resolution has already happened so it does not need to wait.
In the next described method, if the class is using Google Fonts for the font server, it uses
add_action('wp_head', '\AWonderPHP\AWMFontBuilder::gfontDnsPreload', 4);
That is why the above described method needs to be public and is best as static. Plugin and theme developers should generally not call the above method themselves.
public function addWebfontToHead(string $name = ”)
This is the method that adds the
<link /> tag to the head so that the browser will make use of the web fonts. This method should be called only after all the the font families have been defined.
$name parameter should be the name of your theme or plugin, so that the appropriate id attribute can be created. If it is not defined, then a random string is used.