wp_print_font_faces()WP 6.4.0

Generates and prints @font-face CSS rules for the specified fonts.

If no fonts are passed, they are read from the active theme's theme.json file. The CSS is printed inside a <style class="wp-fonts-local"> element.

No Hooks.

Returns

null.

  • void - prints CSS and returns nothing.

Usage

wp_print_font_faces( $fonts );
$fonts(array[][])

Font families and their faces.

Default: []

  • ...$0(array)
    Indexed or associative array of faces for one font family.

    • ...$0(array)
      Properties of one font face.

      • font-family(string)
        Font family name.

      • src(string|string[])
        Font file URL or an array of URLs.

      • font-style(string)
        Font style.
        Default: 'normal'

      • font-weight(string)
        Font weight.
        Default: '400'

      • font-display(string)
        How the font is displayed while it loads.
        Default: 'fallback'

      • ascent-override(string)
        Overrides the height above the baseline.

      • descent-override(string)
        Overrides the depth below the baseline.

      • font-stretch(string)
        Font stretch.

      • font-variant(string)
        Font face variant.

      • font-feature-settings(string)
        OpenType font feature settings.

      • font-variation-settings(string)
        Variable font axis settings.

      • line-gap-override(string)
        Overrides the line gap.

      • size-adjust(string)
        Scales the font outlines and metrics.

      • unicode-range(string)
        Unicode character range for which the font is used.

Examples

0

#1 Output your own font

add_action( 'wp_head', 'my_print_font_faces', 50 );

function my_print_font_faces(): void {
	wp_print_font_faces( [
		'my-font' => [
			[
				'font-family' => 'My Font',
				'src'         => get_theme_file_uri( 'assets/fonts/my-font.woff2' ),
				'font-weight' => '400',
				'font-style'  => 'normal',
				'font-display' => 'swap',
			],
		],
	] );
}

Changelog

Since 6.4.0 Introduced.

wp_print_font_faces() code WP 7.0.4

function wp_print_font_faces( $fonts = array() ) {

	if ( empty( $fonts ) ) {
		$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
	}

	if ( empty( $fonts ) ) {
		return;
	}

	$wp_font_face = new WP_Font_Face();
	$wp_font_face->generate_and_print( $fonts );
}