WP_Theme_JSON::get_viewport_breakpoint_value_in_pixels( mixed $value ): float|null

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

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

Description

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.

Parameters

$valuemixedrequired
Viewport breakpoint size.

Return

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

Source

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;
}

Changelog

VersionDescription
7.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.