wppaste
WordPress

wp_get_computed_fluid_typography_value( array $args = array() ): string|null

Since
6.1.0, 6.3.0, 6.5.0
Source
wp-includes/block-supports/typography.php:464
Internal implementation of CSS clamp() based on available min/max viewport width and min/max font sizes.

Compatibility

WordPress
since 6.5.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

$argsarrayoptional
An associative array of values to calculate a fluid formula for font size. Default is empty array.Default: array()
  • $maximum_viewport_widthstring

    Maximum size up to which type will have fluidity.
  • $minimum_viewport_widthstring

    Minimum viewport size from which type will have fluidity.
  • $maximum_font_sizestring

    Maximum font size for any clamp() calculation.
  • $minimum_font_sizestring

    Minimum font size for any clamp() calculation.
  • $scale_factorint

    A scale factor to determine how fast a font scales within boundaries.

Return value

string|null
A font-size value using clamp() on success, otherwise null.

Performance profile

How much work a call to wp_get_computed_fluid_typography_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
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
31–98

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always31–38wp_get_typography_value_and_unit(), coerce_to()
always50–62wp_get_typography_value_and_unit(), coerce_to(), wp_get_typography_value_and_unit(), coerce_to(), coerce_to()
!empty($linear_factor_denominator)91–98wp_get_typography_value_and_unit(), coerce_to(), wp_get_typography_value_and_unit(), coerce_to(), coerce_to(), round(), round()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10231–9812
8.510231–9812
8.410231–98123 fewer instructions than PHP 8.3
8.310531–10112
8.210531–10112
8.110531–10112
7.410531–10112

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 · 1

Used by · 1

Source code

function wp_get_computed_fluid_typography_value( $args = array() ) {	$maximum_viewport_width_raw = $args['maximum_viewport_width'] ?? null;	$minimum_viewport_width_raw = $args['minimum_viewport_width'] ?? null;	$maximum_font_size_raw      = $args['maximum_font_size'] ?? null;	$minimum_font_size_raw      = $args['minimum_font_size'] ?? null;	$scale_factor               = $args['scale_factor'] ?? null; 	// Normalizes the minimum font size in order to use the value for calculations.	$minimum_font_size = wp_get_typography_value_and_unit( $minimum_font_size_raw ); 	/*	 * We get a 'preferred' unit to keep units consistent when calculating,	 * otherwise the result will not be accurate.	 */	$font_size_unit = $minimum_font_size['unit'] ?? 'rem'; 	// Normalizes the maximum font size in order to use the value for calculations.	$maximum_font_size = wp_get_typography_value_and_unit(		$maximum_font_size_raw,		array(			'coerce_to' => $font_size_unit,		)	); 	// Checks for mandatory min and max sizes, and protects against unsupported units.	if ( ! $maximum_font_size || ! $minimum_font_size ) {		return null;	} 	// Uses rem for accessible fluid target font scaling.	$minimum_font_size_rem = wp_get_typography_value_and_unit(		$minimum_font_size_raw,		array(			'coerce_to' => 'rem',		)	); 	// Viewport widths defined for fluid typography. Normalize units.	$maximum_viewport_width = wp_get_typography_value_and_unit(		$maximum_viewport_width_raw,		array(			'coerce_to' => $font_size_unit,		)	);	$minimum_viewport_width = wp_get_typography_value_and_unit(		$minimum_viewport_width_raw,		array(			'coerce_to' => $font_size_unit,		)	); 	// Protects against unsupported units in min and max viewport widths.	if ( ! $minimum_viewport_width || ! $maximum_viewport_width ) {		return null;	} 	// Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.	$linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];	if ( empty( $linear_factor_denominator ) ) {		return null;	} 	/*	 * Build CSS rule.	 * Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.	 */	$view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;	$linear_factor          = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );	$linear_factor_scaled   = round( $linear_factor * $scale_factor, 3 );	$linear_factor_scaled   = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;	$fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)"; 	return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)";}

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.

6.5.0
Returns early when min and max viewport subtraction is zero to avoid division by zero.from the docblock
6.3.0
Checks for unsupported min/max viewport values that cause invalid clamp values.from the docblock
6.1.0
Introduced.from the docblock

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.