wp_get_typography_value_and_unit( string|int|float $raw_value, array $options = array() ): array|null
- Since
- 6.1.0
- Source
wp-includes/block-supports/typography.php:368
'value' and 'unit', e.g. array( '42', 'rem' ).Compatibility
- WordPress
- since 6.1.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
$raw_valuestring|int|float- Raw size value from theme.json.
$optionsarrayoptional- An associative array of options. Default is empty array.Default:
array()$coerce_tostringdefault: 'rem'Coerce the value to rem or px.$root_size_valueintdefault: 16Value of root font size for rem|em <-> px conversion.$acceptable_unitsstring[]default: array( 'rem', 'px', 'em' );An array of font size units.
Return value
array|null- An array consisting of
'value'and'unit'properties on success.
nullon failure.
Performance profile
How much work a call to wp_get_typography_value_and_unit() 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–75
- Plugin surface
- None
- Called by
- 2
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 87.
Nothing here hands control to plugin code.
2 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_value_and_unit()
Further down the call graph this can also reach query, option, cache, 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_typography_value_and_unit() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($raw_value) | 7–11 | none |
!is_string($raw_value) && !is_int($raw_value) && !is_float($raw_value) | 17 | __(), _doing_it_wrong() |
!empty($raw_value) | 21–34 | wp_parse_args(), preg_match() |
!empty($raw_value) && preg_match() && $unit | 45–75 | wp_parse_args(), preg_match(), round() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 7–75 | 17 | |
| 8.5 | 87 | 7–75 | 17 | |
| 8.4 | 87 | 7–75 | 17 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 92 | 7–80 | 17 | |
| 8.2 | 92 | 7–80 | 17 | |
| 8.1 | 92 | 7–80 | 17 | 1 more instruction than PHP 7.4 |
| 7.4 | 91 | 7–79 | 17 |
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 · 3
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
Used by · 2
- wp_get_computed_fluid_typography_value()Internal implementation of CSS clamp() based on available min/max viewport width and min/max font sizes.
- wp_get_typography_font_size_value()Returns a font-size value based on a given font-size preset.
Source code
function wp_get_typography_value_and_unit( $raw_value, $options = array() ): ?array { if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) { _doing_it_wrong( __FUNCTION__, __( 'Raw size value must be a string, integer, or float.' ), '6.1.0' ); return null; } if ( empty( $raw_value ) ) { return null; } // Converts numbers to pixel values by default. if ( is_numeric( $raw_value ) ) { $raw_value = $raw_value . 'px'; } $defaults = array( 'coerce_to' => '', 'root_size_value' => 16, 'acceptable_units' => array( 'rem', 'px', 'em' ), ); /** * @var array{ * coerce_to: string, * root_size_value: positive-int, * acceptable_units: non-empty-array<non-empty-string>, * } $options */ $options = wp_parse_args( $options, $defaults ); // Bails out if the raw value can't be parsed. if ( ! preg_match( '/^(\d*\.?\d+)([a-zA-Z]+|%)$/', $raw_value, $matches ) ) { return null; } $value = (float) $matches[1]; $unit = $matches[2]; if ( ! in_array( $unit, $options['acceptable_units'], true ) ) { return null; } /* * Default browser font size. Later, possibly could inject some JS to * compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`. */ if ( 'px' === $options['coerce_to'] && ( 'em' === $unit || 'rem' === $unit ) ) { $value = $value * $options['root_size_value']; $unit = $options['coerce_to']; } if ( 'px' === $unit && ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) ) { $value = $value / $options['root_size_value']; $unit = $options['coerce_to']; } /* * No calculation is required if swapping between em and rem yet, * since we assume a root size value. Later we might like to differentiate between * :root font size (rem) and parent element font size (em) relativity. */ if ( ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) && ( 'em' === $unit || 'rem' === $unit ) ) { $unit = $options['coerce_to']; } return array( 'value' => round( $value, 3 ), 'unit' => $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.