Wordpress

Shortcode is not working in block templates?2 min read

May 19, 2023 2 min read

author:

Array

Shortcode is not working in block templates?2 min read

Reading Time: 2 minutes

WordPress block themes offer a powerful and flexible way to create custom layouts and designs. However, recent updates to the WordPress core have caused some issues with shortcodes not rendering as expected in block templates. In this article, we’ll explore the problem, its impact on websites using block themes, and provide a solution to overcome this issue.

Understanding the Issue

The Problem: After updating to WordPress 6.2.1, users of block themes noticed that shortcodes placed within their block templates, such as on the homepage, were no longer rendering as intended. Instead, the shortcode text itself was being displayed, leading to a poor user experience.

Explanation: This issue arose due to a security concern addressed in the WordPress core update. To prevent potential zero-day vulnerabilities, support for shortcodes in block templates was intentionally removed. While this security measure helps protect WordPress websites, it introduced compatibility problems for those relying on shortcodes within their block themes.

Impact on Websites and Users

Effects of the Issue: For websites using block themes, this shortcode problem can disrupt the intended functionality and appearance of their content. Elements such as dynamic lists, custom forms, or interactive components relying on shortcodes might cease to work as expected. This can result in frustrated users and a negative impact on the overall user experience.

The Solution

Implementing a Workaround: Fortunately, there is a workaround that allows block themes to restore shortcode support within block templates. By adding a code snippet to the theme’s functions.php file, you can regain shortcode functionality and resolve the issue.


  <?php
  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);
  ?>

The provided code snippet leverages the render_block_data filter to modify the parsed block data. It checks for the presence of shortcode content within both innerContent and innerBlocks and processes them using the do_shortcode() function. This ensures that the shortcodes within block templates are executed and rendered correctly.

Array