wppaste
WordPress

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
Checks a string for a unit and value and returns an array consisting of '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: 16

    Value 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.
null on 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

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–75

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
empty($raw_value)7–11none
!is_string($raw_value) && !is_int($raw_value) && !is_float($raw_value)17__(), _doing_it_wrong()
!empty($raw_value)21–34wp_parse_args(), preg_match()
!empty($raw_value) && preg_match() && $unit45–75wp_parse_args(), preg_match(), round()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev877–7517
8.5877–7517
8.4877–75175 fewer instructions than PHP 8.3
8.3927–8017
8.2927–8017
8.1927–80171 more instruction than PHP 7.4
7.4917–7917

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

Used by · 2

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.

  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.

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.