Skip to content
PHP

How To Convert HTML Bootstrap Menu In WordPress

A
Author
Jun 6, 2020 · 4 min read
How To Convert HTML Bootstrap Menu In WordPress

If you are building a WordPress theme and want to integrate a Bootstrap menu, you need to customize the WordPress menu structure. By default, WordPress generates a menu structure that doesn’t fully match Bootstrap’s format. However, using the Walker Class, we can transform the WordPress menu into a Bootstrap-compatible menu.

In this article, we’ll go step by step on how to use a custom Walker class to create a Bootstrap menu in WordPress.


Why Use a Walker Class for Menus?

A Walker Class in WordPress is a way to customize the HTML output of hierarchical structures such as menus, categories, and comments. In this case, we’ll use a Walker class to modify the WordPress menu markup to align with Bootstrap’s dropdown structure.


Step 1: Define a Custom Walker Class in functions.php

We will create a custom class named submenu_wrapper by extending the Walker_Nav_Menu class. This will help us customize the output of WordPress menus.

Add the following code to your functions.php file:

class submenu_wrapper extends Walker_Nav_Menu {
    // Add classes to <ul> sub-menus
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = ($depth > 0 ? str_repeat("\t", $depth) : ''); // Code indent
        $display_depth = ($depth + 1); // First submenu is depth 0

        // Define classes for sub-menus
        $classes = array('dropdown-menu');
        $class_names = implode(' ', $classes);

        // Build the submenu HTML
        if ($display_depth == 1) {
            $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
        } else {
            $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
        }
    }
}

What This Code Does:

  • Extends the Walker_Nav_Menu class.
  • Modifies the submenu (<ul>) output by adding the Bootstrap class dropdown-menu.
  • Ensures nested menus work correctly.

Step 2: Add Bootstrap-Compatible Classes to Menu Links

To make our menu fully compatible with Bootstrap, we need to modify the <a> tags inside our menu items. WordPress provides the nav_menu_link_attributes filter to modify menu link attributes.

Add the following function to your functions.php file:

add_filter('nav_menu_link_attributes', 'anchor_menu_add_class', 10, 4);
function anchor_menu_add_class($atts, $item, $args, $depth = 0) {
    // Check if the menu item has children and is at depth 0
    if ($args->walker->has_children == 1 && $depth === 0) {
        $atts['href'] = !empty($item->url) ? $item->url : '';
        $atts['data-toggle'] = 'dropdown';
        $atts['class'] = 'dropdown-toggle';
        $atts['aria-haspopup'] = 'true';
        $atts['aria-expanded'] = 'false';
    } elseif ($depth === 0) {
        $atts['href'] = !empty($item->url) ? $item->url : '';
        $atts['class'] = 'nav-link';
    } elseif ($depth === 1) {
        $atts['href'] = !empty($item->url) ? $item->url : '';
        $atts['class'] = 'dropdown-item';
    }
    return $atts;
}

What This Code Does:

  • Adds Bootstrap’s dropdown-toggle class to parent menu items with children.
  • Ensures top-level items have the nav-link class.
  • Adds the dropdown-item class to submenu links.
  • Uses data-toggle="dropdown" for Bootstrap functionality.

Step 3: Call the Walker Class in header.php

Now that we have our Walker Class and custom attributes for menu links, we need to call it when rendering the menu in header.php.

Replace the default wp_nav_menu() function with the following:

<?php 
echo wp_nav_menu(array(
    'menu' => 'Home Menu',
    'menu_class' => 'navbar-nav ml-auto',
    'walker' => new submenu_wrapper(),
    'container' => false
)); 
?>

What This Code Does:

  • Calls the WordPress menu named "Home Menu".
  • Uses the custom walker class (submenu_wrapper).
  • Adds the Bootstrap class navbar-nav ml-auto.
  • Removes the default div container (container => false).

Step 4: Add Active Class to the Current Menu Item

To highlight the active menu item, WordPress assigns the class current-menu-item. However, we can make it more Bootstrap-friendly.

Add this code to functions.php:

add_filter('nav_menu_css_class', 'add_active_class_to_menu', 10, 2);
function add_active_class_to_menu($classes, $item) {
    if (in_array('current-menu-item', $classes)) {
        $classes[] = 'active';
    }
    return $classes;
}

What This Code Does:

  • Checks if the menu item has the current-menu-item class.
  • If so, it adds the Bootstrap active class to highlight it.

Step 5: Add Bootstrap Navbar Structure

Make sure your menu is wrapped inside the Bootstrap navbar structure in header.php:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Your Logo</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
        aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <?php 
        echo wp_nav_menu(array(
            'menu' => 'Home Menu',
            'menu_class' => 'navbar-nav ml-auto',
            'walker' => new submenu_wrapper(),
            'container' => false
        )); 
        ?>
    </div>
</nav>

Final Thoughts

By following these steps, we have successfully converted a WordPress menu into a Bootstrap menu using the Walker Class.

Key Features Implemented:

  • Used a Walker Class to modify the menu structure.
  • Added Bootstrap classes (dropdown-menu, dropdown-toggle, dropdown-item).
  • Integrated the menu into Bootstrap’s navbar.
  • Added an active class to highlight the current menu item.

Bootstrap Menu


A
Author

Author at The Creative Coders.

The monthly build log

One email a month. New posts, lessons, and the occasional strong opinion. No spam.