wp_render_typography_support()WP 6.1.0

Renders typography styles/content to the block wrapper.

No Hooks.

Returns

String. Filtered block content.

Usage

wp_render_typography_support( $block_content, $block );
$block_content(string) (required)
Rendered block content.
$block(array) (required)
Block object.

Changelog

Since 6.1.0 Introduced.

wp_render_typography_support() code WP 6.9

function wp_render_typography_support( $block_content, $block ) {
	if ( ! empty( $block['attrs']['fitText'] ) && $block['attrs']['fitText'] && ! is_admin() ) {
		wp_enqueue_script_module( '@wordpress/block-editor/utils/fit-text-frontend' );

		// Add Interactivity API directives for fit text to work with client-side navigation.
		if ( ! empty( $block_content ) ) {
			$processor = new WP_HTML_Tag_Processor( $block_content );
			if ( $processor->next_tag() ) {
				if ( ! $processor->get_attribute( 'data-wp-interactive' ) ) {
					$processor->set_attribute( 'data-wp-interactive', true );
				}
				$processor->set_attribute( 'data-wp-context---core-fit-text', 'core/fit-text::{"fontSize":""}' );
				$processor->set_attribute( 'data-wp-init---core-fit-text', 'core/fit-text::callbacks.init' );
				$processor->set_attribute( 'data-wp-style--font-size', 'core/fit-text::context.fontSize' );
				$block_content = $processor->get_updated_html();
			}
		}
		// fitText supersedes any other typography features
		return $block_content;
	}
	if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
		return $block_content;
	}

	$custom_font_size = $block['attrs']['style']['typography']['fontSize'];
	$fluid_font_size  = wp_get_typography_font_size_value( array( 'size' => $custom_font_size ) );

	/*
	 * Checks that $fluid_font_size does not match $custom_font_size,
	 * which means it's been mutated by the fluid font size functions.
	 */
	if ( ! empty( $fluid_font_size ) && $fluid_font_size !== $custom_font_size ) {
		// Replaces the first instance of `font-size:$custom_font_size` with `font-size:$fluid_font_size`.
		return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 );
	}

	return $block_content;
}