WP_Theme_JSON::get_viewport_breakpoint_value_in_pixelsprivate staticWP 7.1.0

Converts a valid viewport breakpoint size to pixels for ordering checks.

Generated media queries keep the original units. This method only normalizes values so mobile and tablet can be compared safely. em and rem lengths use a 16px base for comparison.

Method of the class: WP_Theme_JSON{}

No Hooks.

Returns

float|null. Viewport breakpoint size in pixels, or null when invalid.

Usage

$result = WP_Theme_JSON::get_viewport_breakpoint_value_in_pixels( $value );
$value(mixed) (required)
Viewport breakpoint size.

Changelog

Since 7.1.0 Introduced.

WP_Theme_JSON::get_viewport_breakpoint_value_in_pixels() code WP 7.1

private static function get_viewport_breakpoint_value_in_pixels( $value ) {
	if ( ! static::is_valid_viewport_breakpoint_size( $value ) ) {
		return null;
	}

	$value = trim( $value );
	$unit  = substr( $value, -3 );
	if ( 'rem' === $unit ) {
		$number = (float) substr( $value, 0, -3 );
	} else {
		$unit   = substr( $value, -2 );
		$number = (float) substr( $value, 0, -2 );
	}

	/*
	 * Use the most common browser default font size as the base for em/rem
	 * media query conversions. This pixel value is only used to compare
	 * breakpoint order; generated media queries keep the original units.
	 */
	return 'px' === $unit ? $number : $number * 16;
}