Managing Fonts in WordPress Block Themes
悦文天下08月14日:三个人把我弄得走不动路_湘潭天气网_五月婷婷丁香起综合✈据官方通报,坍塌体育馆建筑面积约1200平方米,墙体为网架结构,顶棚为混凝土板。46DKkhcx-VSHDYS7123FID-WMp7bZjr
Fonts are a defining characteristic of the design of any site. That includes WordPress themes, where it’s common for theme developers to integrate a service like Google Fonts into the WordPress Customizer settings for a “classic” PHP-based theme. That hasn’t quite been the case for WordPress block themes. While integrating Google Fonts into classic themes is well-documented, there’s nothing currently available for block themes in the WordPress Theme Handbook.
That’s what we’re going to look at in this article. Block themes can indeed use Google Fonts, but the process for registering them is way different than what you might have done before in classic themes.
What we already know
As I said, there’s little for us to go on as far as getting started. The Twenty Twenty-Two theme is the first block-based default WordPress theme, and it demonstrates how we can use downloaded font files as assets in the theme. But it’s pretty unwieldy because it involves a couple of steps: (1) register the files in the functions.php
file and (2) define the bundled fonts in the theme.json
file.
Since Twenty Twenty-Two was released, though, the process has gotten simpler. Bundled fonts can now be defined without registering them, as shown in the Twenty Twenty-Three theme. However, the process still requires us to manually download font files and bundle them into the themes. That’s a hindrance that sort of defeats the purpose of simple, drop-in, hosted fonts that are served on a speedy CDN.
What’s new
If you didn’t already know, the Gutenberg project is an experimental plugin where features being developed for the WordPress Block and Site Editor are available for early use and testing. In a recent Theme Shaper article, Gutenberg project lead architect Matias Ventura discusses how Google Fonts — or any other downloaded fonts, for that matter — can be added to block themes using the Create Block Theme plugin.
This short video at Learn WordPress provides a good overview of the Create Block Theme plugin and how it works. But the bottom line is that it does what it says on the tin: it creates block themes. But it does it by providing controls in the WordPress UI that allow you to create an entire theme, child theme, or a theme style variation without writing any code or ever having to touch template files.
I’ve given it a try! And since Create Block Theme is authored and maintained by the WordPress.org team, I’d say it’s the best direction we have for integrating Google Fonts into a theme. That said, it’s definitely worth noting that the plugin is in active development. That means things could change pretty quickly.
Before I get to how it all works, let’s first briefly refresh ourselves with the “traditional” process for adding Google Fonts to classic WordPress themes.
How it used to be done
This ThemeShaper article from 2014 provides an excellent example of how we used to do this in classic PHP themes, as is this newer Cloudways article by Ibad Ur Rehman.
To refresh our memory, here is an example from the default Twenty Seventeen theme showing how Google fonts are enqueued in the functions.php
file.
function twentyseventeen_fonts_url() {
$fonts_url = '';
/**
* Translators: If there are characters in your language that are not
* supported by Libre Franklin, translate this to 'off'. Do not translate
* into your own language.
*/
$libre_franklin = _x( 'on', 'libre_franklin font: on or off', 'twentyseventeen' );
if ( 'off' !== $libre_franklin ) {
$font_families = array();
$font_families[] = 'Libre Franklin:300,300i,400,400i,600,600i,800,800i';
$query_args = array(
'family' => urlencode( implode( '|', $font_families ) ),
'subset' => urlencode( 'latin,latin-ext' ),
);
$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
}
return esc_url_raw( $fonts_url );
}
Then Google Fonts is pre-connected to the theme like this:
function twentyseventeen_resource_hints( $urls, $relation_type ) {
if ( wp_style_is( 'twentyseventeen-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
$urls[] = array(
'href' => 'https://fonts.gstatic.com',
'crossorigin',
);
}
return $urls;
}
add_filter( 'wp_resource_hints', 'twentyseventeen_resource_hints', 10, 2 );
What’s wrong with the traditional way
Great, right? There’s a hitch, however. In January 2022, a German regional court imposed a fine on a website owner for violating Europe’s GDPR requirements. The issue? Enqueuing Google Fonts on the site exposed a visitor’s IP address, jeopardizing user privacy. CSS-Tricks covered this a while back.
The Create Block Theme plugin satisfies GDPR privacy requirements, as it leverages the Google Fonts API to serve solely as a proxy for the local vendor. The fonts are served to the user on the same website rather than on Google’s servers, protecting privacy. WP Tavern discusses the German court ruling and includes links to guides for self-hosting Google Fonts.
How to use Google Fonts with block themes
This brings us to today’s “modern” way of using Google Fonts with WordPress block themes. First, let’s set up a local test site. I use Flywheel’s Local app for local development. You can use that or whatever you prefer, then use the Theme Test Data plugin by the WordPress Themes Team to work with dummy content. And, of course, you’ll want the Create Block Theme plugin in there as well.
Have you installed and activated those plugins? If so, navigate to Appearance → Manage theme fonts from the WordPress admin menu.
The “Manage theme fonts” screen displays a list of any fonts already defined in the theme’s theme.json
file. There are also two options at the top of the screen:
- Add Google fonts. This option adds Google Fonts directly to the theme from the Google fonts API.
- Add local fonts. This option adds downloaded font files to the theme.
I’m using a completely blank theme by WordPress called Emptytheme. You’re welcome to roll along with your own theme, but I wanted to call out that I’ve renamed Emptytheme to “EMPTY-BLANK” and modified it, so there are no predefined fonts and styles at all.
I thought I’d share a screenshot of my theme’s file structure and theme.json
file to show that there are literally no styles or configurations going on.
Let’s click the “Add Google Fonts” button. It takes us to a new page with options to choose any available font from the current Google Fonts API.
For this demo, I selected Inter from the menu of options and selected the 300, Regular, and 900 weights from the preview screen:
Once I’ve saved my selections, the Inter font styles I selected are automatically downloaded and stored in the theme’s assets/fonts
folder:
Notice, too, how those selections have been automatically written to the theme.json
file in that screenshot. The Create Block Theme plugin even adds the path to the font files.
View the entire theme.json
code
{
"version": 2,
"settings": {
"appearanceTools": true,
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
},
"typography": {
"fontFamilies": [
{
"fontFamily": "Inter",
"slug": "inter",
"fontFace": [
{
"fontFamily": "Inter",
"fontStyle": "normal",
"fontWeight": "300",
"src": [
"file:./assets/fonts/inter_300.ttf"
]
},
{
"fontFamily": "Inter",
"fontStyle": "normal",
"fontWeight": "900",
"src": [
"file:./assets/fonts/inter_900.ttf"
]
},
{
"fontFamily": "Inter",
"fontStyle": "normal",
"fontWeight": "400",
"src": [
"file:./assets/fonts/inter_regular.ttf"
]
}
]
}
]
}
}
}
If we go to the Create Block Theme’s main screen and click the Manage theme fonts button again, we will see Inter’s 300, 400 (Regular), and 900 weight variants displayed in the preview panel.
A demo text preview box at the top even allows you to preview the selected fonts within the sentence, header, and paragraph with the font size selection slider. You can check out this new feature in action in this GitHub video.
The selected font(s) are also available in the Site Editor Global Styles (Appearance → Editor), specifically in the Design panel.
From here, navigate to Templates → Index and click the blue Edit button to edit the index.html
template. We want to open the Global Styles settings, which are represented as a contrast icon located at the top-right of the screen. When we click the Text settings and open the Font menu in the Typography section… we see Inter!
Same thing, but with local fonts
We may as well look at adding local fonts to a theme since the Create Block Theme plugin provides that option. The benefit is that you can use any font file you want from whatever font service you prefer.
Without the plugin, we’d have to grab our font files, drop them somewhere in the theme folder, then resort to the traditional PHP route of enqueuing them in the functions.php
file. But we can let WordPress carry that burden for us by uploading the font file on the Add local fonts screen using the Create Block Theme interface. Once a file is selected to upload, font face definitions boxes are filled automatically.
Even though we can use any .ttf
, .woff
, or .woff2
file, I simply downloaded Open Sans font files from Google Fonts for this exercise. I snatched two weight variations, regular and 800.
The same auto-magical file management and theme.json
update we saw with the Google Fonts option happens once again when we upload the font files (which are done one at a time). Check out where the fonts landed in my theme folder and how they are added to theme.json
:
Removing fonts
The plugin also allows us to remove font files from a block theme from the WordPress admin. Let’s delete one of the Open Sans variants we installed in the last section to see how that works.
Clicking the Remove links triggers a warning for you to confirm the deletion. We’ll click OK to continue.
Let’s open our theme folder and check the theme.json
file. Sure enough, the Open Sans 800 file we deleted on the plugin screen removed the font file from the theme folder, and the reference to it is long gone in theme.json
.
There’s ongoing work happening
There’s talk going on adding this “Font Manager” feature to WordPress Core rather than needing a separate plugin.
An initial iteration of the feature is available in the repo, and it uses the exact same approach we used in this article. It should be GDPR-compliant, too. The feature is scheduled to land with WordPress 6.3 release later this year.
Wrapping up
The Create Block Theme plugin significantly enhances the user experience when it comes to handling fonts in WordPress block themes. The plugin allows us to add or delete any fonts while respecting GDPR requirements.
We saw how selecting a Google Font or uploading a local font file automatically places the font in the theme folder and registers it in the theme.json
file. We also saw how the font is an available option in the Global Styles settings in the Site Editor. And if we need to remove a font? The plugin totally takes care of that as well — without touching theme files or code.
Thanks for reading! If you have any comments or suggestions, share them in the comments. I’d love to know what you think of this possible direction for font management in WordPress.
Additional resources
I relied on a lot of research to write this article and thought I’d share the articles and resources I used to provide you with additional context.
WordPress font management
- How to add typographic fonts to WordPress block themes (Theme Shaper)
- How to add Google fonts to WordPress themes (Theme Shaper)
- How to Use Google Fonts With WordPress? (Cloudways)
- Manage your block theme fonts with Create Block Theme (Learn WordPress)
- Using Create Block Theme (WordPress.tv)
GitHub issues
- Global Styles/Typography: Managing font sets (#45271)
- Font manager (#46332)
European GDPR requirements
- German Court Fines Website Owner for Violating the GDPR by Using Google-Hosted Fonts (WPTavern)
- Complying with GDPR when using Google Fonts (Make WordPress Themes)
- German court awards user €100 in damages against website operator for using Google Fonts (ePrivacy Blog)
- Bunny Fonts (CSS-Tricks)
Managing Fonts in WordPress Block Themes originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
(但)【dan】(更)【geng】(多)【duo】(产)【chan】(品)【pin】(出)【chu】(现)【xian】(延)【yan】(期)【qi】(募)【mu】(集)【ji】(的)【de】(窘)【jiong】(况)【kuang】(。)【。】(据)【ju】(统)【tong】(计)【ji】(,)【,】(2)【2】(0)【0】(2)【2】(3)【3】(年)【nian】(上)【shang】(半)【ban】(年)【nian】(,)【,】(全)【quan】(市)【shi】(场)【chang】(共)【gong】(有)【you】(1)【1】(7)【7】(2)【2】(只)【zhi】(基)【ji】(金)【jin】(宣)【xuan】(布)【bu】(延)【yan】(期)【qi】(结)【jie】(束)【shu】(募)【mu】(集)【ji】(,)【,】(更)【geng】(有)【you】(6)【6】(只)【zhi】(基)【ji】(金)【jin】(产)【chan】(品)【pin】(宣)【xuan】(布)【bu】(募)【mu】(集)【ji】(失)【shi】(败)【bai】(。)【。】
【为】「残念ね。けっこうすごいやつなのにc今回のは」と緑はいかにも残念そうに言った。【了】□【便】【于】❤【准】☠【确】「あたり前でしょう」【监】↑【测】 曹操冷冷的瞥了瘫倒在地上的伏完一眼,冷哼一声,甩袖而去,封王,绝不可行,小家伙鼠目寸光,若真的封王了,那他这个皇帝还有什么用?【不】 “勇敢和鲁莽,只有一线之隔。”吕布抬眼看了儿子一眼,一直冷着的脸上泛起一抹微笑:“无论时机还是出手时的果断都很到位,一击得手之后迅速逃脱,并没有恋战,如果再迟疑半分,以邓展的实力,至少你现在没办法跟我来这里吃饭,做得很不错。”【良】✍【反】【应】☢【,】「ねcワタナベ君」とレイコさんが僕に言った。「悪いけれど二十分くらいそのへんをぶらぶら散歩してきてくれない。そうすればなんとかなると思うから」【尽】◆【量】✯【将】【不】♒【同】¡【疫】✿【苗】ღ【分】「日常生活の中で役に立つということはあまりないね」と僕は言った。「でも具体的に何かの役に立つというよりはcそういうのは物事をより系統的に捉えるための訓練になるんだと僕は思ってるけれど」【开】 “子真兄是为叔桓兄好,长安的客栈,一般世家可真住不起,卫家如今家道中落,能省一点是一点,这长安书院供应各家弟子花费银钱已经不少,实在没有嗟来之食赠予卫兄。”【接】【种】【,】「そうだね」【间】 “古人云,朝闻道,夕死可矣,老夫能在有生之年,得遇冠军侯,幸甚,幸甚。”郑玄呵呵笑道。【隔】【两】┆【周】「私ただ知りたいのよ」と緑は言った。「それに彼にこんなこと訊いたらすごく怒るのよ。女はそんなのいちいち訊くもんじゃないだって」【以】「かわりますよcそれはなんとなく」と僕は言った。【上】☼【最】☏【佳】☣【,】ツ【但】☤【若】↑【在】☢【H】【P】 “军师,那蔡瑁虽然为人所不齿,但其本事却是不差。”刘备也担心的看向诸葛亮,当初在洛阳之时,双方有过一段时间的合作,蔡瑁在统兵之上却有一套。【V】©【疫】 谁坐院长之位,在长安书院内部已经立下了规矩,老的院长如果逝去,新的院长会从学院精英之中选出,能力、弟子,方方面面,郑小同便是有能力,现在也太过年轻,不适合坐这个位子,要知道如今长安书院可不是刚刚建立时人才凋零,哪怕是儒学院之中,能者也不少。【苗】→【接】 “我军战损如何?”张辽面色有些难看,虽然赢了这一仗,但对方推出来的那种怪异的冲城车还是突破了他们的防线,如果没有攻陷邺城的话,后果不堪设想。【种】™【期】「春の熊」と緑はまた頭を上げた。「それ何よc春の熊って」【间】彼女は何回か玉を撞いた。玉筋を見るときの彼女の目は真剣でc玉を撞くときの力の入れ方は正確だった。彼女はきれいにセットした髪をくるりとうしろに回して金のイヤリングを光らせcパンプスの位置をきちんと決めcすらりと伸びた美しい指で台のフェルトを押えて玉を撞く様子を見ているとcうす汚いビリヤード場のそこの場所だけが何かしら立派な社交場の一角であるように見えた。彼女と二人きりになるのは初めてだったがcそれは僕にとって素敵な体験だった。彼女と一緒にいると僕は人生を一段階上にひっぱりあげられたような気がした。三ゲームを終えたところで――もちろん三ゲームめも彼女が圧勝した――僕の手の傷が少しうずきはじめたので我々はゲームを切りあげることにした。【,】¡【出】♪【现】緑は少し酔払っていて階段を一段踏み外してc我々はあやうく下まで転げおちそうになった。店の外に出ると空をうすく覆っていた雲が晴れてc夕暮に近い太陽が街にやさしく光を注いでいた。僕と緑はそんな街をしばらくぶらぶらと歩いた。緑は木のぼりがしたいといったがc新宿にはあいにくそんな木はなかったしc新宿御苑はもう閉まる時間だった。【了】♀【特】 “怎么回事?”看着周围已经聚集了不少百姓,吕布排开人群,皱眉看向衙差班头道。【殊】日が暮れると寮はしんとしてcまるで廃墟みたいな感じになった。国旗がポールから降ろされc食堂の窓に電気が灯った。学生の数が減ったせいでc食堂の灯はいつもの半分しかついていなかった。右半分は消えてc左半分だけがついていた。それでも微かに夕食の匂いが漂っていた。クリームシチューの匂いだった。【情】【况】♪【,】⊙【必】 “我乃骠骑将军坐下破羌中郎将魏延,尔等主将已被我生擒,天兵不日及至,还不快快投降!”魏延倒拖大刀,命人守住城门之后,飞马冲入城中,刀光狂舞,嘴中却是不断大喝。【须】 “妇人之见!”张鲁面色一黑,这还没打呢,就要投降,好歹他也是一路诸侯,传出去,颜面何存?【接】®【种】⊿【其】僕はレイコさんの目を見た。彼女は泣いていた。僕は思わず彼女に口づけした。まわりを通りすぎる人たちは僕たちのことをじろじろとみていたけれどc僕にはもうそんなことは気にならなかった。我々は生きていたしc生きつづけることだけを考えなくてはならなかったのだ。【他】°【疫】 不知道什么时候开始,郑玄变得更老了,如果按照历史轨迹来说,吕布救他的那一年,他其实已经是他的死期了,丧子之痛,被袁绍裹挟,拉上袁家的战车,最后郁郁而终,当时的郑玄,其实已经有了直面死亡的准备。【苗】【,】【则】「いいよ。二時からの授業まではどうせ暇だから」【以】✈【急】↖【症】☑【为】「ときどき自分でも何がどうなっているのかわかんなくなっちゃうことがあるのよ」と直子は言った。【主】♋【,】僕は顔を上げてもう一度相手の顔をよく見た。しかし何度見ても見覚えはなかった。彼女はとても目立つの女の子だったしcどこかであっていたらすぐ思い出せるはずだった。それに僕の名前を知っている人間はそれほどたくさんこの大学にいるわけではない。【按】【需】「日曜日はいつも何をしてるの」【接】◤【种】♛【即】◆【可】【。】┆黄色小说在线阅读-家核优居手机站 - 国内首家专业智能家居。