W3cubDocs

/WordPress

WP_Font_Utils::maybe_add_quotes( string $item ): string

Adds surrounding quotes to font family names that contain special characters.

Description

It follows the recommendations from the CSS Fonts Module Level 4.

Parameters

$itemstringrequired
A font family name.

Return

string The font family name with surrounding quotes, if necessary.

Source

private static function maybe_add_quotes( $item ) {
	// Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens).
	$regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/';
	$item  = trim( $item );
	if ( preg_match( $regex, $item ) ) {
		$item = trim( $item, "\"'" );
		return '"' . $item . '"';
	}
	return $item;
}

Changelog

Version Description
6.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_font_utils/maybe_add_quotes