wppaste
WordPress

_wp_theme_json_webfonts_handler()

Since
6.0.0
Source
wp-includes/deprecated.php:5417
Runs the theme.json webfonts handler.

Description

Using WP_Theme_JSON_Resolver, it gets the fonts defined in the theme.json for the current selection and style variations, validates the font-face properties, generates the '@font-face' style declarations, and then enqueues the styles for both the editor and front-end.

Design Notes: This is not a public API, but rather an internal handler.
A future public Webfonts API will replace this stopgap code.

This code design is intentional.
a. It hides the inner-workings.
b. It does not expose API ins or outs for consumption.
c. It only works with a theme's theme.json.

Why? a. To avoid backwards-compatibility issues when the Webfonts API is introduced in Core.
b. To make fontFace declarations in theme.json work.

Compatibility

Deprecated in WordPress 6.4.0. Use wp_print_font_faces() instead.

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to _wp_theme_json_webfonts_handler() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–11

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 512.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below _wp_theme_json_webfonts_handler()

Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_theme_json_webfonts_handler() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$styles === ""7none
$styles !== ""11wp_add_inline_style()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5127–1169
8.55127–1169
8.45127–116922 fewer instructions than PHP 8.3
8.35347–1169
8.25347–1169
8.15347–11695 fewer instructions than PHP 7.4
7.45397–1169

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 19

Show all 19

Source code

function _wp_theme_json_webfonts_handler() {	_deprecated_function( __FUNCTION__, '6.4.0', 'wp_print_font_faces' ); 	// Block themes are unavailable during installation.	if ( wp_installing() ) {		return;	} 	if ( ! wp_theme_has_theme_json() ) {		return;	} 	// Webfonts to be processed.	$registered_webfonts = array(); 	/**	 * Gets the webfonts from theme.json.	 *	 * @since 6.0.0	 *	 * @return array Array of defined webfonts.	 */	$fn_get_webfonts_from_theme_json = static function() {		// Get settings from theme.json.		$settings = WP_Theme_JSON_Resolver::get_merged_data()->get_settings(); 		// If in the editor, add webfonts defined in variations.		if ( is_admin() || wp_is_rest_endpoint() ) {			$variations = WP_Theme_JSON_Resolver::get_style_variations();			foreach ( $variations as $variation ) {				// Skip if fontFamilies are not defined in the variation.				if ( empty( $variation['settings']['typography']['fontFamilies'] ) ) {					continue;				} 				// Initialize the array structure.				if ( empty( $settings['typography'] ) ) {					$settings['typography'] = array();				}				if ( empty( $settings['typography']['fontFamilies'] ) ) {					$settings['typography']['fontFamilies'] = array();				}				if ( empty( $settings['typography']['fontFamilies']['theme'] ) ) {					$settings['typography']['fontFamilies']['theme'] = array();				} 				// Combine variations with settings. Remove duplicates.				$settings['typography']['fontFamilies']['theme'] = array_merge( $settings['typography']['fontFamilies']['theme'], $variation['settings']['typography']['fontFamilies']['theme'] );				$settings['typography']['fontFamilies']          = array_unique( $settings['typography']['fontFamilies'] );			}		} 		// Bail out early if there are no settings for webfonts.		if ( empty( $settings['typography']['fontFamilies'] ) ) {			return array();		} 		$webfonts = array(); 		// Look for fontFamilies.		foreach ( $settings['typography']['fontFamilies'] as $font_families ) {			foreach ( $font_families as $font_family ) { 				// Skip if fontFace is not defined.				if ( empty( $font_family['fontFace'] ) ) {					continue;				} 				// Skip if fontFace is not an array of webfonts.				if ( ! is_array( $font_family['fontFace'] ) ) {					continue;				} 				$webfonts = array_merge( $webfonts, $font_family['fontFace'] );			}		} 		return $webfonts;	};

Changelog

Introduced in 6.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.4.0
Deprecated. Use wp_print_font_faces() instead.from the docblock
6.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/deprecated.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.