* Widget API: WP_Widget_Block class
* Core class used to implement a Block widget.
class WP_Widget_Block extends WP_Widget {
protected $default_instance = array(
* Sets up a new Block widget instance.
public function __construct() {
'classname' => 'widget_block',
'description' => __( 'A widget containing a block.' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
parent::__construct( 'block', __( 'Block' ), $widget_ops, $control_ops );
add_filter( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
* Outputs the content for the current Block widget instance.
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Block widget instance.
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->default_instance );
$this->get_dynamic_classname( $instance['content'] ),
* Filters the content of the Block widget before output.
* @param string $content The widget content.
* @param array $instance Array of settings for the current widget.
* @param WP_Widget_Block $widget Current Block widget instance.
echo $args['after_widget'];
* Calculates the classname to use in the block widget's container HTML.
* 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.
* @param string $content The HTML content of the current block widget.
* @return string The classname to use in the block widget's container HTML.
private function get_dynamic_classname( $content ) {
$blocks = parse_blocks( $content );
$block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
$classname = 'widget_block widget_text';
$classname = 'widget_block widget_calendar';
$classname = 'widget_block widget_search';
$classname = 'widget_block widget_custom_html';
$classname = 'widget_block widget_archive';
case 'core/latest-posts':
$classname = 'widget_block widget_recent_entries';
case 'core/latest-comments':
$classname = 'widget_block widget_recent_comments';
$classname = 'widget_block widget_tag_cloud';
$classname = 'widget_block widget_categories';
$classname = 'widget_block widget_media_audio';
$classname = 'widget_block widget_media_video';
$classname = 'widget_block widget_media_image';
$classname = 'widget_block widget_media_gallery';
$classname = 'widget_block widget_rss';
$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.
* @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,
return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
* Handles updating settings for the current Block widget instance.
* @param array $new_instance New settings for this instance as input by the user via
* @param array $old_instance Old settings for this instance.
* @return array Settings to save or bool false to cancel saving.
public function update( $new_instance, $old_instance ) {
$instance = array_merge( $this->default_instance, $old_instance );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['content'] = $new_instance['content'];
$instance['content'] = wp_kses_post( $new_instance['content'] );
* Outputs the Block widget settings form.
* @see WP_Widget_Custom_HTML::render_control_template_scripts()
* @param array $instance Current instance.
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, $this->default_instance );
<label for="<?php echo $this->get_field_id( 'content' ); ?>">
/* translators: HTML code of the block, not an option that blocks HTML. */
<textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" rows="6" cols="50" class="widefat code"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
* Makes sure no block widget is considered to be wide.
* @param bool $is_wide Whether the widget is considered wide.
* @param string $widget_id Widget ID.
* @return bool Updated `is_wide` value.
public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
if ( str_starts_with( $widget_id, 'block-' ) ) {