Block Navigation in WordPress: Converting Classic Menus

From classic menus to the Navigation block in a block theme

A classic theme registers menu locations and prints them with wp_nav_menu(); the menus are edited in Appearance → Menus. A block theme has no menu locations: the header template part contains a Navigation block, and the menu items are stored in a wp_navigation post. When a site switches themes, the old menus are still there, and WordPress can import them, but the details decide whether the new menu really matches the old one.

Problem: A site moves from a classic theme to a block theme. The new header shows a default page list instead of the main menu, the mobile menu behaves differently, and a custom walker that added icons to menu items has no equivalent.

Solution: Import the classic menu into the Navigation block (the block offers it, or point the block to it with a ref after converting), check the result in the Site Editor, move walker-based changes to the render_block_core/navigation-link filter or block styles, and set the overlay (mobile menu) behavior on the block.

Where menus live now

Classic menus are terms of the taxonomy nav_menu with items as nav_menu_item posts. The Navigation block stores its items as block markup in a post of the type wp_navigation:

<!-- wp:navigation {"ref":42,"overlayMenu":"mobile","layout":{"type":"flex","justifyContent":"right"}} /-->

ref is the ID of that post. The same navigation can be used in several template parts; editing it in one place changes it everywhere.

Import the classic menu

When a Navigation block without a ref is selected in the Site Editor, its menu (the “⋮” options in the block toolbar or the block settings) lists the classic menus under “Import Classic Menus”. Choosing one creates a wp_navigation post with the same items and links the block to it. Submenus become Submenu blocks, custom links stay custom links, pages and categories become Navigation Link blocks with their IDs, so later changes of the page title or slug are reflected.

What does not come along:

  • CSS classes from the “CSS Classes” field of menu items are imported as className and still work if the CSS exists in the new theme.
  • Output of custom walkers, nav_menu_item_title and similar filters does not apply to the block.
  • Menu locations disappear; the block in each template part decides which menu is shown.

Replace a walker with a block filter

A walker that added an icon before menu items with a certain class becomes a render filter:

add_filter( 'render_block_core/navigation-link', 'ha_nav_icon', 10, 2 );
function ha_nav_icon( string $html, array $block ): string {
    $classes = $block['attrs']['className'] ?? '';
    if ( ! str_contains( $classes, 'ha-icon-cart' ) ) {
        return $html;
    }
    $processor = new WP_HTML_Tag_Processor( $html );
    if ( $processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) {
        $processor->add_class( 'has-ha-icon' );
    }
    return $processor->get_updated_html();
}
.has-ha-icon::before {
    content: "";
    display: inline-block;
    inline-size: 1em;
    block-size: 1em;
    margin-inline-end: .4em;
    background: currentColor;
    mask: url(images/cart.svg) no-repeat center / contain;
}

The filter runs for every Navigation Link block when the page renders. WP_HTML_Tag_Processor (WordPress 6.2+) changes attributes safely without regular expressions on HTML. The icon itself comes from CSS, so it follows the link color.

Mobile menu

The Navigation block brings its own overlay menu: overlayMenu is mobile (default: a hamburger button below 600 px), always or never. The breakpoint is fixed in core CSS. If the design needs the overlay at a larger width, use always on a second navigation block that is shown only on smaller screens, rather than overriding core’s media query.

Keep classic menus usable for classic code

A plugin or a widget that still calls wp_nav_menu( array( 'menu' => 'Footer' ) ) keeps working: the classic menus are not deleted by the import. The Navigation block does not write back to them, so after the switch, edit menus only in the Site Editor and delete the old classic menus when nothing uses them anymore. How walkers worked in classic themes: different walker menus for header and footer.

NOTE: A Navigation block without ref and without inner blocks shows a fallback: the most recently created wp_navigation post, or a page list. That is why a fresh block theme shows “all pages” in the header until a menu is chosen. Block theme templates and parts: block theme templates.