Last active
March 13, 2025 21:39
-
-
Save charmoney/d9e5b19b26f80cdc00e21c04a994271f to your computer and use it in GitHub Desktop.
WordPress Full Site Editor navigation blocks don't render shortcodes. This gist will execute shortcodes in FSE navigation block attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace charmoney; | |
/** | |
* Execute shortcodes in FSE navigation block attributes. | |
* | |
* Supports wp:navigation-link and wp:navigation-submenu plus potentially others. | |
* | |
* @param array $items Navigation block items. | |
* @return array Filtered block items. | |
*/ | |
function block_core_navigation_render_inner_blocks( $items ) { | |
$shortcode_attrs = array( | |
'url', | |
'title', | |
'label', | |
); | |
// Loop through the items top level. | |
foreach ( $items as $key => $item ) { | |
// Execute shortcodes in top layer nav items. | |
foreach ( $shortcode_attrs as $shortcode_attr ) { | |
if ( isset( $item->parsed_block['attrs'][ $shortcode_attr ] ) && ( false !== mb_stripos( $item->parsed_block['attrs'][ $shortcode_attr ], '[' ) || false !== mb_stripos( $item->parsed_block['attrs'][ $shortcode_attr ], '%5B' ) ) ) { | |
$item->parsed_block['attrs'][ $shortcode_attr ] = do_shortcode( urldecode( $item->parsed_block['attrs'][ $shortcode_attr ] ), true ); | |
} | |
} | |
// Inner loop through $item->inner_blocks. | |
foreach ( $item->inner_blocks as $inner_key => $inner_item ) { | |
// Execute shortcodes in second layer nav items. | |
// NO SUPPORT for lower layers, just two. | |
foreach ( $shortcode_attrs as $shortcode_attr ) { | |
if ( isset( $inner_item->parsed_block['attrs'][ $shortcode_attr ] ) && ( false !== mb_stripos( $inner_item->parsed_block['attrs'][ $shortcode_attr ], '[' ) || false !== mb_stripos( $inner_item->parsed_block['attrs'][ $shortcode_attr ], '%5B' ) ) ) { | |
$inner_item->parsed_block['attrs'][ $shortcode_attr ] = do_shortcode( urldecode( $inner_item->parsed_block['attrs'][ $shortcode_attr ] ), true ); | |
} | |
} | |
} | |
} | |
return $items; | |
} | |
add_filter( 'block_core_navigation_render_inner_blocks', __NAMESPACE__ . '\block_core_navigation_render_inner_blocks' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment