0

So I have updated WP to 5.8 and the widget titles are no longer being recognized from my customized functions.php

'name'          => __( 'Sidebar 1', 'mytheme' ),
'id'            => 'sidebar-1',
'before_widget' => '<div id="sidebar">',
'before_title' => '<h1 class="sidebar-title"><span>',
'after_title' => '</span></h1>',
'after_widget' => '</div>',

I'm racking my brains and I can't seem to solve it because it's a new change and no one seems to have asked this anywhere. My widgets want to have <h2>Title</h2> no matter what I put in functions. There is no sign of my H1 class. Plus massive spaces/padding because I think the titles ("headings" now apparently) are outside of my widget blocks. If i revert back to an older WP version, it works just fine. I can't use any plugins. Am I missing something obvious?

1

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 :(

2
  • Genius, thank you! It's working for me until when/if they will change this. (Voted up but I don't have enough reputation here.) I'm not liking this new block widget update but getting used to it. Aug 25 at 10:27
  • Me neither. By the way, you gave me the idea to test if "Classic Widgets" (that came with the new system, it's the equivalent to "Classic Editor" for the editor) fix the issue, but that's not the case, unfortunately.
    – Pierre R
    Aug 25 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.