Find out in this tutorial how to fix the bug that causes shortcodes not to run in block templates in WordPress 6.2.1.
Have you upgraded to WordPress 6.2.1 and suddenly some pages are not displayed correctly?
Well, it’s not just you, it’s a known bug and I hope they will fix it in the next version (probably 6.2.2), but that may take a few weeks yet.
The bug affects themes that use block templates and is due to the fact that the new version (6.2.1) prevents shortcodes from running inside them. The change was intended to fix a vulnerability, but it has created an unexpected problem. Let’s see how to fix it.
Snippet to fix shortcodes bug introduced by WordPress 6.2.1 in block templates
Since the WordPress 6.2.1 update fixes several security bugs I do not recommend you to go back to the previous version (6.2), but to use the temporary patch that WordPress core contributor Anderson Martins has shared.
To do this, add the following code at the end of functions.php or in your functionality plugin:
// Restore shortcode support in block templates
add_filter('render_block_data', function($parsed_block) {
if (isset($parsed_block['innerContent'])) {
foreach ($parsed_block['innerContent'] as &$innerContent) {
if (empty($innerContent)) {
continue;
}
$innerContent = do_shortcode($innerContent);
}
}
if (isset($parsed_block['innerBlocks'])) {
foreach ($parsed_block['innerBlocks'] as $key => &$innerBlock) {
if (! empty($innerBlock['innerContent'])) {
foreach ($innerBlock['innerContent'] as &$innerContent) {
if (empty($innerContent)) {
continue;
}
$innerContent = do_shortcode($innerContent);
}
}
}
}
return $parsed_block;
}, 10, 1);
Done, shortcodes inside template blocks will display correctly again.
When the new WordPress update comes out fixing this bug you will be able to remove this code snippet.
Conclusion
If you have encountered shortcodes not running in your template blocks after the update to WordPress 6.2.1, now you know how to fix it with code.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉