Legacy Widget does not use `before_widget` and `before_title` from registered sidebar
When a title is set in a "Legacy Widget", the HTML tags output by the front side and the editor side are different.
Furthermore, even on the front side, if it is inside a group block, etc., the default output will be used.
Prerequisites
Consider the following situation where the sidebar area is set up as shown below.
register_sidebar(
array(
'name' => 'Sidebar',
'id' => 'sidebar-1',
'before_widget' => '<div id="%1$s" class="c-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="c-widget__title">',
'after_title' => '</div>',
)
);
And suppose we apply the following CSS.
.c-widget {
border: solid 1px #000;
padding: 1rem;
}
.c-widget__title{
background: #000;
color: #fff;
margin: -1rem -1rem 1.25rem;
padding: .5em;
}
At this time, the design expected when the "Text Widget" is placed is as follows.
Problem 1 - About previews in the editor
The HTML of the title becomes h2.widgettitle.
Problem 2 - About when a widget is inside a Group block
At this time, the HTML of the title becomes h2.widgettitle on the front side as well.
This problem is introduced in #32723 and also occurs in Column blocks.
Expected output:

Actual output:

Hi @ddryo, thanks for the issue and sample code. I confirmed this bug locally.
This happens because the Legacy Widget block will call the_widget to render the widget without passing before_widget, before_title, etc.
https://github.com/WordPress/gutenberg/blob/314c9844055220833692bd08c4d1735aca136784/packages/widgets/src/blocks/legacy-widget/index.php#L45-L48
It should be possible to change Legacy Widget to pass these args. The main difficulty, though, is that Legacy Widget has to obtain before_widget, before_title, etc. from the sidebar. Legacy Widget currently does not have any knowledge about what sidebar it is in. Moreover, it is possible to use a Legacy Widget block in the post editor or site editor where there is no sidebar.
We will have to get creative. Maybe have sidebarId as an optional block attribute (see also https://github.com/WordPress/gutenberg/issues/33638) or note the sidebar that is currently being rendered using hooks (which is what Widget Group does).
Reproduced on Gutenberg trunk at 59b54cfc3e7 with Twenty Twenty-Five.
Setup
- A sidebar registered with
before_titleset to<div class="c-widget__title">andafter_titleset to</div>. - A widget area for that sidebar containing a Group block, with a Legacy Widget block (Text widget with a title) inside the Group.
- The sidebar rendered on the front end with
dynamic_sidebar().
Result
- Expected title markup:
<div class="c-widget__title">Title</div> - Actual title markup:
<h2 class="widgettitle">Title</h2>
The widget area's own wrapper does use the registered markup. Only the nested Legacy Widget ignores it.
PR raised: WordPress/gutenberg#84071
Fixes the title markup for a Legacy Widget nested in a Group or Columns block in a widget area. It now uses the sidebar's before_title and after_title instead of the default h2.widgettitle.
Status: This is a partial fix, so the PR uses See #33507. Not covered: the editor preview (Problem 1 in the issue), and before_widget / after_widget.