Customising widget titles/headings in WP 5.8

You’re right. I didn’t notice it before !

If you look at the code, you can see that Widgets are now showed from /wp-includes/widgets/class-wp-widget-block.php which extends /wp-includes/class-wp-widget.php

The first file is a new one introduced in WP 5.8.
In there you can see that the widget is now displayed through widget_block_content filter which use the do_blocks() function (/wp-includes/blocks.php)

I don’t know if it is an issue or not but you can use this new hook described above, like so :

function my_widget_block_content($content) {
    $replacements = [
        '<h2>' => '<div class="h1">',
        '</h2>' => '</div>',
    ];
    $content = str_replace(array_keys($replacements), $replacements, $content);
    return $content;
}
    
add_filter('widget_block_content', 'my_widget_block_content');

I hope it will be changed because the str_replace() function is used on the whole content of the widget and if you are using, for exemple, a WYSIWYG and you write un Heading 2 in this, it will be replaced as well 🙁

Leave a Comment