wp_get_typography_font_size_value( array $preset, bool|array $settings = array() ): string|null
- Since
- 6.1.0, 6.1.1, 6.2.0, 6.3.0, 6.4.0, 6.6.0, 6.7.0
- Source
wp-includes/block-supports/typography.php:565
Description
Takes into account fluid typography parameters and attempts to return a CSS formula depending on available, valid values.
Compatibility
- WordPress
- since 6.7.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.
Parameters
$presetarray- Required. fontSizes preset value as seen in theme.json.
$namestringName of the font size preset.$slugstringKebab-case, unique identifier for the font size preset.$sizestring|int|floatCSS font-size value, including units if applicable.
$settingsbool|arrayoptional- Optional Theme JSON settings array that overrides any global theme settings.
Default is false.Default:array()
Return value
string|null- Font-size value or null if a size is not passed in $preset.
Performance profile
How much work a call to wp_get_typography_font_size_value() 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
- Light
- Scaling
- Constant
- Instructions
- 11–142
- Plugin surface
- None
- Called by
- 5
Only touches the object cache, via wp_cache_get().
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 183.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wp_get_typography_font_size_value() - cacheobject cache
wp_cache_get()one call below wp_get_typography_font_size_value()
Further down the call graph this can also reach query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_typography_font_size_value() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($preset) && !$preset && $fluid_font_size_settings === false | 11 | none |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !isset($layout_settings) && !isset($fluid_settings) && empty($preferred_size) | 65–70 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && empty($preferred_size) | 71–76 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), wp_get_typography_value_and_unit() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !isset($layout_settings) && !isset($fluid_settings) && !empty($preferred_size) && !empty($minimum_font_size_limit) | 80–85 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), coerce_to() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !isset($layout_settings) && !isset($fluid_settings) && !empty($preferred_size) | 86–102 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), coerce_to(), minimum_viewport_width() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !empty($preferred_size) && !empty($minimum_font_size_limit) | 86–91 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), wp_get_typography_value_and_unit(), coerce_to() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !empty($preferred_size) | 92–108 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), wp_get_typography_value_and_unit(), coerce_to(), minimum_viewport_width() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !isset($layout_settings) && !isset($fluid_settings) && !empty($preferred_size) | 115–136 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), coerce_to(), log(), clamp(), round(), minimum_viewport_width() |
isset($preset) && !$preset && $fluid_font_size_settings !== false && !empty($preset) && !is_bool($settings) && !$settings && !empty($typography_settings) && !$typography_settings && !$fluid_settings && !empty($preferred_size) | 121–142 | wp_get_global_settings(), wp_parse_args(), wp_get_typography_value_and_unit(), wp_get_typography_value_and_unit(), coerce_to(), log(), clamp(), round(), minimum_viewport_width() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 177 | 11–136 | 29 | 6 fewer instructions than PHP 8.5 |
| 8.5 | 183 | 11–142 | 29 | |
| 8.4 | 183 | 11–142 | 29 | |
| 8.3 | 183 | 11–142 | 29 | |
| 8.2 | 183 | 11–142 | 29 | |
| 8.1 | 183 | 11–142 | 29 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 187 | 11–144 | 29 |
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 · 7
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- wp_get_global_settings()Gets the settings resulting of merging core, theme, and user data.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_get_typography_value_and_unit()Checks a string for a unit and value and returns an array consisting of `'value'` and `'unit'`, e.g. array( '42', 'rem' ).
- clamp()Polyfill for `clamp()` function added in PHP 8.6.
- wp_get_computed_fluid_typography_value()Internal implementation of CSS clamp() based on available min/max viewport width and min/max font sizes.
Used by · 5
- WP_Theme_JSON::compute_style_properties()Given a styles array, it extracts the style properties and adds them to the $declarations array following the format:
- block_core_navigation_link_build_css_font_sizes()Build an array with CSS classes and inline styles defining the font sizes which will be applied to the navigation markup in the front-end.
- get_typography_styles_for_block_core_search()Returns typography styles to be included in an HTML style tag.
- wp_apply_typography_support()Adds CSS classes and inline styles for typography features such as font sizes to the incoming attributes array. This will be applied to the block markup in the front-end.
- wp_render_typography_support()Renders typography styles/content to the block wrapper.
Source code
function wp_get_typography_font_size_value( $preset, $settings = array() ) { if ( ! isset( $preset['size'] ) ) { return null; } /* * Catches falsy values and 0/'0'. Fluid calculations cannot be performed on `0`. * Also returns early when a preset font size explicitly disables fluid typography with `false`. */ $fluid_font_size_settings = $preset['fluid'] ?? null; if ( false === $fluid_font_size_settings || empty( $preset['size'] ) ) { return $preset['size']; } /* * As a boolean (deprecated since 6.6), $settings acts as an override to switch fluid typography "on" (`true`) or "off" (`false`). */ if ( is_bool( $settings ) ) { _deprecated_argument( __FUNCTION__, '6.6.0', __( '`boolean` type for second argument `$settings` is deprecated. Use `array()` instead.' ) ); $settings = array( 'typography' => array( 'fluid' => $settings, ), ); } // Fallback to global settings as default. $global_settings = wp_get_global_settings(); $settings = wp_parse_args( $settings, $global_settings ); $typography_settings = $settings['typography'] ?? array(); /* * Return early when fluid typography is disabled in the settings, and there * are no local settings to enable it for the individual preset. * * If this condition isn't met, either the settings or individual preset settings * have enabled fluid typography. */ if ( empty( $typography_settings['fluid'] ) && empty( $fluid_font_size_settings ) ) { return $preset['size']; } $fluid_settings = $typography_settings['fluid'] ?? array(); $layout_settings = $settings['layout'] ?? array(); // Defaults. $default_maximum_viewport_width = '1600px'; $default_minimum_viewport_width = '320px'; $default_minimum_font_size_factor_max = 0.75; $default_minimum_font_size_factor_min = 0.25; $default_scale_factor = 1; $default_minimum_font_size_limit = '14px'; // Defaults overrides. $minimum_viewport_width = $fluid_settings['minViewportWidth'] ?? $default_minimum_viewport_width; $maximum_viewport_width = isset( $layout_settings['wideSize'] ) && ! empty( wp_get_typography_value_and_unit( $layout_settings['wideSize'] ) ) ? $layout_settings['wideSize'] : $default_maximum_viewport_width; if ( isset( $fluid_settings['maxViewportWidth'] ) ) { $maximum_viewport_width = $fluid_settings['maxViewportWidth']; } $has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( wp_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) ); $minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit; // Try to grab explicit min and max fluid font sizes. $minimum_font_size_raw = $fluid_font_size_settings['min'] ?? null; $maximum_font_size_raw = $fluid_font_size_settings['max'] ?? null; // Font sizes. $preferred_size = wp_get_typography_value_and_unit( $preset['size'] ); // Protects against unsupported units. if ( empty( $preferred_size['unit'] ) ) { return $preset['size']; } /* * Normalizes the minimum font size limit according to the incoming unit,Changelog
Introduced in 6.1.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.1.0 tag, from
src/wp-includes/block-supports/typography.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.