Build a custom nav menu walker for Bootstrap dropdowns
wp_nav_menu() prints a menu as nested <ul> and <li> elements with WordPress classes. CSS frameworks expect their own markup: Bootstrap’s navbar wants nav-item and nav-link classes, a dropdown-menu list and a toggle with data-toggle="dropdown". The class that writes the menu HTML is Walker_Nav_Menu, and a child class can change exactly the parts you need.
Problem: The default menu markup does not work with Bootstrap 4 dropdowns, and adding classes with filters is not enough because the sub-menu and the toggle need different markup.
Solution: Extend Walker_Nav_Menu, override start_lvl() for the sub-menu and start_el() for each item, keep the WordPress item classes, and pass an instance of the class in the walker argument of wp_nav_menu().
The walker
<?php
class HA_Bootstrap_Nav_Walker extends Walker_Nav_Menu {
// The <ul> of a sub-menu
public function start_lvl( &$output, $depth = 0, $args = null ) {
$output .= '<ul class="dropdown-menu">';
}
// One menu item: the <li> and its link
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$has_children = in_array( 'menu-item-has-children', $classes, true );
$is_active = in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-ancestor', $classes, true );
$li_classes = $classes; // keep the WordPress classes (menu-item-123, current-menu-item...)
if ( 0 === $depth ) {
$li_classes[] = 'nav-item';
}
if ( $has_children && 0 === $depth ) {
$li_classes[] = 'dropdown';
}
$output .= '<li class="' . esc_attr( implode( ' ', array_filter( $li_classes ) ) ) . '">';
$atts = array(
'href' => $item->url,
'target' => $item->target,
'rel' => $item->xfn,
'title' => $item->attr_title,
'class' => $depth > 0 ? 'dropdown-item' : 'nav-link',
);
if ( $is_active ) {
$atts['class'] .= ' active';
$atts['aria-current'] = 'page';
}
if ( $has_children && 0 === $depth ) {
$atts['class'] .= ' dropdown-toggle';
$atts['data-toggle'] = 'dropdown';
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false';
}
$html = '';
foreach ( $atts as $name => $value ) {
if ( '' !== (string) $value ) {
$value = 'href' === $name ? esc_url( $value ) : esc_attr( $value );
$html .= ' ' . $name . '="' . $value . '"';
}
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$output .= '<a' . $html . '>' . $title . '</a>';
}
}
end_lvl() and end_el() of the parent class close </ul> and </li>, so they need no change. Keeping $item->classes matters: they include the classes editors enter in the menu screen (“CSS Classes” option) and the current-* classes.
Using it
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'container' => false,
'menu_class' => 'navbar-nav mr-auto',
'depth' => 2, // Bootstrap dropdowns have one level
'fallback_cb' => false,
'walker' => new HA_Bootstrap_Nav_Walker(),
) );
Bootstrap’s dropdown toggle opens the menu on click and does not follow the link, so the parent item’s own page is not reachable from the toggle. Either use parents without pages (“Custom Link” with #) or repeat the parent page as the first item of its sub-menu.
The same walker can produce different markup for different menus when it checks $args->theme_location, as shown in different walker output for the header and the footer. Registering the menu locations is covered in registering and displaying navigation menus.
Update (2021): Bootstrap 5 renamed the data attributes to data-bs-toggle="dropdown" and the margin utility mr-auto to me-auto, and no longer needs jQuery. The walker stays the same apart from these names.
NOTE: When you only need extra classes or attributes, a walker is more than necessary: the nav_menu_css_class, nav_menu_link_attributes and nav_menu_submenu_css_class filters change them without replacing the whole output.