Scenario:
Gutenberg Blocks -> page.html has wp: include that includes a pattern file that has shortcode in it, the shortcode is not executed and comes back text only.
After reviewing and testing the above issue I realized the shorcode just does not run, I did find a workaround that allows the shortcode to run as expected.
Solution:
Created my own tag that does the work and is formatted close to the original
replace this:
<!-- wp:pattern {"slug":my-theme/hidden-my-include"} /-->
With this:
<!-- wp:shortcode --> [wpb_run_pattern pattern="hidden-my-include"] <!-- /wp:shortcode -->
Then create a file in your /patterns/ folder called hidden-my-include.php
Then add this code to it:
<!-- wp:shortcode --> [wpb_run_misc_shortcode] <!-- /wp:shortcode -->
Then, in your functions.php file put this code and run your files:
function run_pattern_shortcode_from_block_func($atts) {
if (!isset($atts['pattern']) || !is_string($atts['pattern'])) {
return 'Invalid input.';
}
// Sanitize the input
$pattern_name = sanitize_text_field($atts['pattern']);
// Construct the file path using WordPress functions
$file_path = get_stylesheet_directory() . '/patterns/' . $pattern_name . '.php';
// Check if file exists and is a PHP file
if (file_exists($file_path) && pathinfo($file_path, PATHINFO_EXTENSION) === 'php') {
$content = file_get_contents($file_path);
$blocks = parse_blocks($content);
$processed_content="";
foreach ($blocks as $block) {
$block_content = render_block($block);
$block_content = do_shortcode($block_content);
$processed_content .= $block_content;
}
return $processed_content;
} else {
return 'File not found.';
}
}
add_shortcode('wpb_run_pattern', 'run_pattern_shortcode_from_block_func');
function wpb_run_misc_shortcode_func() {
// Example of escaping output
return esc_html('Hello World !!!');
}
add_shortcode('wpb_run_misc_shortcode', 'wpb_run_misc_shortcode_func');
The shortcode in the patterns file ran as expected.