W3cubDocs

/WordPress

WP_Font_Utils::sanitize_font_family( string $font_family ): string

Sanitizes and formats font family names.

Description

  • 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.

See also

Parameters

$font_familystringrequired
Font family name(s), comma-separated.

Return

string Sanitized and formatted font family name(s).

Source

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

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/sanitize_font_family