Fonts chosen under a classic theme keep loading after switching to a block theme

Open
#53,167 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
performance

Research direction

Start by reading Jetpack_Fonts::maybe_render_fonts() and get_font_css(); the issue does not name their file or any tests. Check how the method handles empty CSS and the jetpack_fonts_render_without_rules filter, then verify that themes without applicable font rules emit neither the loader nor the style while themes with rules still do.

Written by the indexing model from the issue text.

Description

Bug Jetpack Agent Backlog
Impacted plugin

Wpcomsh

Quick summary

If a user uses a classic theme, and adds custom fonts in the customizer, then switches to a block theme, those added fonts will still be loaded for visitors, even if they aren't used. And the site admin cannot remove the fonts as the block theme now hides access to the customizer.

Custom Fonts stores the chosen fonts in the site option jetpack_fonts (selected_fonts), whatever theme is active. Jetpack_Fonts::maybe_render_fonts() prints the WebFontConfig loader and webfont.js whenever that option has fonts. If the active theme has no font annotations (any block theme, for example), the generated font CSS is empty, so nothing on the page uses the fonts. But webfont.js still downloads them on every page view. On a block theme the Customizer isn't in the Appearance menu, so site owners can't clear the setting themselves.

Steps to reproduce
  1. On a classic theme with font annotations, choose a heading and a body font in Customizer → Fonts, then publish.
  2. Switch to Twenty Twenty-Five.
  3. View the front-end page source.

Expected: no Custom Fonts output, because the theme has no rules to apply the fonts.
Actual: a WebFontConfig block and webfont.js load both fonts (through fonts-api.wp.com on WordPress.com), and <style id="jetpack-custom-fonts-css"> is empty.

Site owner impact

Fewer than 20% of the total website/platform users

Severity

Minor

What other impact(s) does this issue have?

No response

If a workaround is available, please outline it here.
Cause
  • maybe_render_fonts() checks only get_fonts() (plus Customizer preview and AMP) before calling output_webfont_loader().
  • get_font_css() returns no rules for themes without annotations, but the loader runs anyway.
  • Nothing clears or ignores selected_fonts when the theme changes. (gutenberg-integration.php already skips block themes for editor styles.)
Impact

Unused font downloads plus an extra script on every page view, on any site that picked fonts under an annotated theme and later moved to a theme without annotations. Reported by an Atomic customer on Twenty Twenty-Five, who had Arimo and Lato loading for nothing.

Proposed fix

In maybe_render_fonts(), build the font CSS first, and return before printing the loader if it is empty. See the comments below for details, and for why no data backfill is needed.

public function maybe_render_fonts( $force = false ) {
	if ( ( is_customize_preview() && ! $force ) || ! $this->get_fonts() || ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ) {
		return;
	}

	$css = $this->get_font_css();
	// Without theme rules for the chosen fonts, nothing on the page uses them.
	if ( '' === trim( $css ) && ! apply_filters( 'jetpack_fonts_render_without_rules', false ) ) {
		return;
	}

	$webfont_options = $this->get_webfont_options();
	if ( count( (array) $webfont_options ) > 0 ) {
		$this->output_webfont_loader( $webfont_options );
	}

	echo '<style id="jetpack-custom-fonts-css">' . $css . "</style>\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Why this, and why no backfill:

  • The check runs at page render, so it fixes every affected site as soon as it deploys. No option data needs migrating.
  • Deleting selected_fonts in bulk would be worse. It can't be undone, and a site that switches back to its classic theme would lose fonts it chose deliberately. With the guard, those picks simply start working again.
  • It's precise. It skips output only when no rule applies to any chosen font, whatever the theme type. It also covers classic themes without annotations, which have the same waste.

One risk to check first: someone might have picked a font in Custom Fonts and then used it in their own Additional CSS, relying on the loader to fetch it. The guard would stop loading that font. The jetpack_fonts_render_without_rules filter is an escape hatch for those sites. Before shipping, it's worth sampling how many sites have selected_fonts set, no annotation rules, and Additional CSS that mentions one of those family names.

Workaround

Delete the stale selection: wp option patch delete jetpack_fonts selected_fonts.

Platform (Simple and/or Atomic)

Atomic

Dominant language
PHP
Stars
1.9k
Forks
913
Avg merge
1d 18h
Merged PRs (30d)
809

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Automattic/jetpack

All issues in Automattic/jetpack

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.