WP_Font_Utils::sanitize_font_familypublic staticWP 6.5.0

Sanitizes and formats font family names.

  • Applies sanitize_text_field.
  • Adds surrounding quotes to names containing any characters that are not alphabetic or dashes.

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

Method of the class: WP_Font_Utils{}

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Returns

String. Sanitized and formatted font family name(s).

Usage

$result = WP_Font_Utils::sanitize_font_family( $font_family );
$font_family(string) (required)
Font family name(s), comma-separated.

Notes

Changelog

Since 6.5.0 Introduced.

WP_Font_Utils::sanitize_font_family() code WP 6.8.1

public static function sanitize_font_family( $font_family ) {
	if ( ! $font_family ) {
		return '';
	}

	$output          = sanitize_text_field( $font_family );
	$formatted_items = array();
	if ( str_contains( $output, ',' ) ) {
		$items = explode( ',', $output );
		foreach ( $items as $item ) {
			$formatted_item = self::maybe_add_quotes( $item );
			if ( ! empty( $formatted_item ) ) {
				$formatted_items[] = $formatted_item;
			}
		}
		return implode( ', ', $formatted_items );
	}
	return self::maybe_add_quotes( $output );
}