_wp_theme_json_webfonts_handler()
- Since
- 6.0.0
- Source
wp-includes/deprecated.php:5416
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
- Scaling
- Constant
- Instructions
- 7–11
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 512.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
$styles === "" | 7 | none |
$styles !== "" | 11 | wp_add_inline_style() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 512 | 7–11 | 69 | |
| 8.5 | 512 | 7–11 | 69 | |
| 8.4 | 512 | 7–11 | 69 | 22 fewer instructions than PHP 8.3 |
| 8.3 | 534 | 7–11 | 69 | |
| 8.2 | 534 | 7–11 | 69 | |
| 8.1 | 534 | 7–11 | 69 | 5 fewer instructions than PHP 7.4 |
| 7.4 | 539 | 7–11 | 69 |
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
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- wp_theme_has_theme_json()Checks whether a theme or its parent has a theme.json file.
- is_admin()Determines whether the current request is for an administrative interface page.
- wp_is_rest_endpoint()Checks whether a REST API endpoint request is currently being handled.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_theme_file_uri()Retrieves the URL of a file in the theme.
- _wp_to_kebab_case()This function is trying to replicate what lodash's kebabCase (JS library) does in the client.
- wp_parse_args()Merges user defined arguments into defaults array.
- __()Retrieves the translation of $text.
- sanitize_url()Sanitizes a URL for database or redirect usage.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Show all 19
- wp_register_style()Registers a CSS stylesheet.
- wp_enqueue_style()Enqueues a CSS stylesheet.
- wp_add_inline_style()Adds extra CSS styles to a registered stylesheet.
- add_action()Adds a callback function to an action hook.
- WP_Theme_JSON_Resolver::get_merged_data()::get_settings()
- WP_Theme_JSON_Resolver::get_merged_data()Returns the data merged from multiple origins.
- WP_Theme_JSON_Resolver::get_style_variations()Returns the style variations defined by the theme.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.