Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

WP_Widget_Block::get_dynamic_classname() WordPress Method

The WP_Widget_Block::get_dynamic_classname() method is used to get a dynamic class name for a block widget. This is useful for making sure that the correct CSS class is applied to a widget when it is rendered on the front-end.

WP_Widget_Block::get_dynamic_classname( string $content ) #

Calculates the classname to use in the block widget’s container HTML.


Description

Usually this is set to $this->widget_options['classname'] by dynamic_sidebar(). In this case, however, we want to set the classname dynamically depending on the block contained by this block widget.

If a block widget contains a block that has an equivalent legacy widget, we display that legacy widget’s class name. This helps with theme backwards compatibility.


Top ↑

Parameters

$content

(string)(Required)The HTML content of the current block widget.


Top ↑

Return

(string) The classname to use in the block widget's container HTML.


Top ↑

Source

File: wp-includes/widgets/class-wp-widget-block.php

	private function get_dynamic_classname( $content ) {
		$blocks = parse_blocks( $content );

		$block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;

		switch ( $block_name ) {
			case 'core/paragraph':
				$classname = 'widget_block widget_text';
				break;
			case 'core/calendar':
				$classname = 'widget_block widget_calendar';
				break;
			case 'core/search':
				$classname = 'widget_block widget_search';
				break;
			case 'core/html':
				$classname = 'widget_block widget_custom_html';
				break;
			case 'core/archives':
				$classname = 'widget_block widget_archive';
				break;
			case 'core/latest-posts':
				$classname = 'widget_block widget_recent_entries';
				break;
			case 'core/latest-comments':
				$classname = 'widget_block widget_recent_comments';
				break;
			case 'core/tag-cloud':
				$classname = 'widget_block widget_tag_cloud';
				break;
			case 'core/categories':
				$classname = 'widget_block widget_categories';
				break;
			case 'core/audio':
				$classname = 'widget_block widget_media_audio';
				break;
			case 'core/video':
				$classname = 'widget_block widget_media_video';
				break;
			case 'core/image':
				$classname = 'widget_block widget_media_image';
				break;
			case 'core/gallery':
				$classname = 'widget_block widget_media_gallery';
				break;
			case 'core/rss':
				$classname = 'widget_block widget_rss';
				break;
			default:
				$classname = 'widget_block';
		}

		/**
		 * The classname used in the block widget's container HTML.
		 *
		 * This can be set according to the name of the block contained by the block widget.
		 *
		 * @since 5.8.0
		 *
		 * @param string $classname  The classname to be used in the block widget's container HTML,
		 *                           e.g. 'widget_block widget_text'.
		 * @param string $block_name The name of the block contained by the block widget,
		 *                           e.g. 'core/paragraph'.
		 */
		return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
	}


Top ↑

Changelog

Changelog
VersionDescription
5.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.