May 252014
Article WordPress

Our previous post explained how to configure the use of the responsive Bootstrap 3 framework in a wordpress theme.

But, setting up a menu as a responsive navigation bar requires some additional settings. This post focuses on how to carry out this task.


Adding a responsive bootstrap menu to a WordPress theme – custom walker class

A menu is inserted in a wordpress template with a call to wp_nav_menu(). This function uses the PHP class Walker_Nav_Menu. Both wp_nav_menu and Walker_Nav_Menu are defined in the wp-includes/nav-menu-template.php script).

The HTML code of a menu, generated by Walker_Nav_Menu, is a tree of unsorted lists using the HTML tags <ul> and <li>,  where each list element is a menu entry. Each <li> tag is assigned a set of CSS classes, that can be used to tailor the design of the menu.

The following example shows the HTML code generated for a threee-level menu:

<div class="menu-main-menu-container">
    <ul id="menu-main-menu" class="menu">
        <li id="menu-item-1499" class="menu-item menu-item-type-custom menu-item-object-custom
                                          current-menu-item current_page_item menu-item-1499">
            <a href="/">Home</a>
        <li id="menu-item-1500" class="menu-item menu-item-type-custom menu-item-object-custom
                                                       menu-item-has-children menu-item-1500">
            <a href="#">Categories</a>
            <ul class="sub-menu">
                <li id="menu-item-1501" class="menu-item menu-item-type-custom
                               menu-item-object-custom menu-item-has-children menu-item-1501">
                    <a href="#">Programming</a>
                    <ul class="sub-menu">
                        <li id="menu-item-1502" class="menu-item menu-item-type-taxonomy                                                             menu-item-object-category menu-item-1502">
                            <a href="">Perl</a>

Bootstrap also implements the responsiveness of menus by means of classes assigned to these tags, only that it uses different class names. Bootstrap also adds some extra elements required to make the menu responsive. For instance, the square button with three horizontal lines that is displayed when the menu is in a collapsed state (The default state for mobile devices with small screens).

Adding these classes and extra elements requires replacing the default WordPress walker with a custom walker class.

Luckyly, we don’t have to do it from scratch. Instead, we can download from a PHP script that defines a PHP class wp-bootstrap-navwalker implementing this functionality. Jeff Mould’work is based on the wp-bootstrap-navwalker class developed by twittem (that can also be downloaded from github). Jeff made some changes to the original class, to support multi-level menus. He has also replaced the use of Glyphicons with the use of Font Awesome icons to enhance the presentation of the menu.

Once the compressed file has been downloaded, its content is extracted under the theme’s directory.

Next, we must add to functions.php the sentence to include this script:


The CSS required needs to be imported as well. This is done adding the import sentence in style.css:

@import url('multi-level-bootstrap-menu-master/style.css');

Finally, the call to wp_nav_menu found in header.php needs to be modified, adding an argument to request the use of the custom walker, and the additional markup:

In header.php, before:

wp_nav_menu( array( 'theme_location' => 'header-menu' ) );


<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle"
              data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>


    wp_nav_menu( array(
        'theme_location'    => 'header-menu',
        'depth'             => 0,
        'container'         => 'div',
        'container_class'   => 'navbar-collapse collapse',
        'container_id'      => 'bs-example-navbar-collapse-1',
        'menu_class'        => 'nav navbar-nav',
        'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
        'walker'            => new wp_bootstrap_navwalker())

In this code, we can see that:

  • The call to wp_nav_menu is now placed inside a <nav> element. This element also contains a button with CSS class “navbar-toggle”. This is the square button with three dashes that is displayed when the menu is collapsed.
  • The array that is passed as argument in the call to wp_nav_menu() also has some additional elements now. Among them, the value of the  ‘walker’ element is an instance fo the custom class wp_bootstrap_navwalker() defined in the previously downloaded script.

With these changes done, we can check the new look of our menu in the browser of a desktop computer:


and the rendering of the menu on a mobile device, when it is collapsed and when it is opened:


collapsed responsive menu


deployed responsive menu



 Posted by at 8:26 am

 Leave a Reply