wp_render_custom_css_class_name()WP 7.0.0

Applies the custom CSS class name to the block's rendered HTML.

The class name is generated in wp_render_custom_css_support_styles() and stored in block attributes. This filter adds it to the actual markup.

No Hooks.

Returns

String. Filtered block content.

Usage

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

Changelog

Since 7.0.0 Introduced.

wp_render_custom_css_class_name() code WP 7.0

function wp_render_custom_css_class_name( $block_content, $block ) {
	$class_name_attr = $block['attrs']['className'] ?? null;

	if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, 'wp-custom-css-' ) ) {
		return $block_content;
	}

	// Parse out the 'wp-custom-css-*' class name added by wp_render_custom_css_support_styles().
	$custom_class_name = null;
	$token_delimiter   = " \t\f\r\n";
	$class_token       = strtok( $class_name_attr, $token_delimiter );
	while ( false !== $class_token ) {
		if ( str_starts_with( $class_token, 'wp-custom-css-' ) ) {
			$custom_class_name = $class_token;
			break;
		}
		$class_token = strtok( $token_delimiter );
	}
	if ( null === $custom_class_name ) {
		return $block_content;
	}

	$tags = new WP_HTML_Tag_Processor( $block_content );

	if ( $tags->next_tag() ) {
		$tags->add_class( 'has-custom-css' );
		$tags->add_class( $custom_class_name );
	}

	return $tags->get_updated_html();
}