`theme.json` Global Styles: font family CSS not output on the frontend
Description
When setting a font family through theme.json, the necessary CSS is not output on the frontend.
Step-by-step reproduction instructions
- Given a website running TT1 blocks.
- Add the following into the
stylessection of thetheme.json:
"typography": {
"fontFamily": "Georgia"
}
- Open the site editor, and see the font being applied.
- Visit the frontend, and see the font missing.
Expected behaviour
The fontFamily style should be applied on the frontend and the backend.
Actual behaviour
The fontFamily style only works in the backend (site editor).
WordPress information
- WordPress version: 5.8.0
- Gutenberg version: 11.1.0
- Are all plugins except Gutenberg deactivated? Yes
- Are you using a default theme (e.g. Twenty Twenty-One)? Yes, TT1
Device information
- Device: Desktop
- Operating system: MacOS
- Browser: Chrome
Hey @fklein-lu,
Based on this documentation here: https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-json/#settings
I think the property is actually called fontFamilies and not fontFamily. If you change that, would it fix the problem?
@adamziel No, we're talking about two different things here.
fontFamilies is a setting and enabled the font family drop down. fontFamily is a style, and styles content.
Thanks for reporting this @fklein-lu, I meant to do it, but never got to it. I believe it is basically everything that Gutenberg supports in theme.json breaks on the frontend if you are running 5.8 (which includes font families).
I think I might also be experiencing this issue also. I am unsure because it's slightly inconsistent for me almost working on my local install but not on hosting. I had such a hard time trying to debug this over the weekend and ended up hardcoding font-family into theme.json which seemed a stepbackward. Everything was working for me before the update.
After much tracing and debugging I found a workaround. https://github.com/bobbingwide/written/issues/12#issuecomment-887435159
Set one or both of WP_DEBUG and SCRIPT_DEBUG to true.
in wp-config.php
define('WP_DEBUG', true);
define( 'SCRIPT_DEBUG', true );
Digging deeper, it's a problem with the caching in gutenberg_experimental_global_styles_get_stylesheet()
Can confirm that this is happening using emptytheme following the same steps from the issue. It can be seen on the theme directory preview, for example: https://wp-themes.com/quadrat/
The debug point rings true for what I was experiencing it being not on my local install and issue but on hosted an issue. So confirming that is the case for me also.
Looks like global style loader is setting transient (https://github.com/WordPress/WordPress/blob/master/wp-includes/script-loader.php#L2313) which is causing this issue.
Setting up WP_DEBUG is skipping styles loading from transient cache https://github.com/WordPress/gutenberg/blob/trunk/lib/global-styles.php#L19-L33.
Both Gutenberg and WordPress attempt to use the same key for the transient data.
Function wp_enqueue_global_styles() in wp-includes/script-loader.php gets and sets the transient
but the data structure doesn't include the fontFamily stuff.
Another workaround is for Gutenberg to use a different key. eg global_styles_GB.
When running Core 5.8 without the plugin, the custom properties for fontFamilies are not output on the frontend, and the select is not displayed on supported blocks. (e.g. Button.) With 5.8 and the Gutenberg plugin active, it works correctly.
I'm not sure whether this is the same problem referenced in this Github issue or not. If not, then I'll be happy to open a new Github issue.
Never mind, I've just seen that fontFamilies is only currently supported with the Plugin, not with Core.
When is fontFamilies merge expected? 5.9?