gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

`theme.json` Global Styles: font family CSS not output on the frontend

Open fklein-lu opened this issue 5 years ago • 12 comments

Description

When setting a font family through theme.json, the necessary CSS is not output on the frontend.

Step-by-step reproduction instructions

  1. Given a website running TT1 blocks.
  2. Add the following into the styles section of the theme.json:
"typography": {
   "fontFamily": "Georgia"
}
  1. Open the site editor, and see the font being applied.
  2. 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

fklein-lu avatar Jul 26 '21 07:07 fklein-lu

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 avatar Jul 26 '21 10:07 adamziel

@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.

fklein-lu avatar Jul 26 '21 11:07 fklein-lu

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).

ndiego avatar Jul 26 '21 15:07 ndiego

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.

karmatosed avatar Jul 26 '21 18:07 karmatosed

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 );

bobbingwide avatar Jul 27 '21 11:07 bobbingwide

Digging deeper, it's a problem with the caching in gutenberg_experimental_global_styles_get_stylesheet()

bobbingwide avatar Jul 27 '21 15:07 bobbingwide

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/

MaggieCabrera avatar Jul 27 '21 16:07 MaggieCabrera

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.

karmatosed avatar Jul 27 '21 16:07 karmatosed

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.

sagarnasit avatar Jul 27 '21 17:07 sagarnasit

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.

bobbingwide avatar Jul 27 '21 17:07 bobbingwide

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.

markhowellsmead avatar Aug 06 '21 09:08 markhowellsmead

When is fontFamilies merge expected? 5.9?

permanyer avatar Jan 10 '22 10:01 permanyer