Skip to content

Bootstrap 5 Walker for Sage 10

6 years, 1 toxic job, and a lot of mental health challenges later I’m back with a new blog post. I’ve been out of work for the past 2.5-3 years while recovering from an illness and I have not built any sites or coded much since I stopped working.

Since I got sick every time I’ve tried to do any sort of programming or web development I’ve just hit brick wall after brick wall. Every time I sit down to do something I feel like I’ve forgotten the little bit I did know and like I have no capacity to retain anything new. I end up getting frustrated and overwhelmed and I give up. I’ve tried getting back up to speed by working on my personal sites with no success until this past couple weeks.

It’s a small thing but putting together this walker is actually the first win I’ve had since getting ill. Since it’s a milestone in my recovery and something others might find useful I thought I’d post it on my blog. It seems like a good reintroduction and considering my last post was about a month after starting the toxic job that wrecked my mental health, I’d like this post to also signify me finally moving on from that bit of my past.

Anyway, this is a WordPress NavWalker intended to be used with the Soil plugin in a Sage 10 project. It extends the nice Soil walker and adds Bootstrap 5 classes. I hope it’s helpful to someone out there, and I hope to be posting here more frequently in the future.

Bootstrap 5 Walker for Sage 10

Adds Bootstrap 5 css classes to the nice Soil nav walker in a Sage 10 project.

Assumptions

  • Assumes you’re using Sage 10.
  • Assumes the Roots/Soil plugin is installed and activated.
  • Assumes Tailwind has been removed from the Sage project and Bootstrap 5 has been added.

Limitations

  • Only supports 1 level of dropdowns.
  • There are probably better versions of this out there.

To use

  • Add BootstrapNav.php to the themes /app directory.
  • Replace the contents of /resources/views/sections/header.blade.php with the version below.

BootstrapNav.php


<?php

namespace App;

use Roots\Soil\NavWalker as SoilNav;

/**
 * Return if Soil does not exist.
 */
if (!class_exists('Roots\Soil\NavWalker')) {
    return;
}

/**
 * Class BootstrapNav
 * Add Bootstrap 5 classes to nav walker
 */
class BootstrapNav extends SoilNav
{

    /**
     * NavWalker constructor.
     */
    public function __construct()
    {
        parent::__Construct();
        add_filter('nav_menu_submenu_css_class', [$this, 'dropdownListClass'], 10, 2);
        add_filter('nav_menu_css_class', [$this, 'listItemClass'], 10, 2);
        add_filter('nav_menu_link_attributes', [$this, 'linkAttributes'], 10, 4);
    }

    /**
     * Add "dropdown-menu" class to dropdown UL
     * @param $classes
     * @return array
     */
    function dropdownListClass($classes): array
    {
        $classes[] = 'dropdown-menu';
        return $classes;
    }

    /**
     * Add Bootstrap 5 classes to list items
     * @param $classes
     * @param $item
     * @return array
     */

    function listItemClass($classes, $item): array
    {
        if ($item->menu_item_parent == 0) {
            $classes[] = 'nav-item';
        }
        if ($item->is_subitem) {
            $classes[] = 'dropdown';
        }
        return $classes;
    }

    /**
     * Add Bootstrap 5 classes and attributes to anchor links.
     * This method originally created by QWp6t (see credit link).
     * @param $atts
     * @param $item
     * @param $args
     * @param $depth
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter) This method overrides its parent
     * @link Credit https://gist.github.com/QWp6t/8f94b7096bb0d3a72fedba68f73033a5#file-bootstrap-php-L63
     */
    public function linkAttributes($atts, $item, /** @noinspection PhpUnusedParameterInspection */ $args, $depth): array
    {
        $atts['class'] = $depth ? 'dropdown-item' : 'nav-link';
        if ($item->current || $item->current_item_ancestor) {
            $atts['class'] .= ' active';
        }
        if ($item->is_subitem) {
            $atts['class'] .= ' dropdown-toggle';
            $atts += [
                'data-bs-toggle' => 'dropdown',
                'role' => 'button',
                'aria-expanded' => 'false'
            ];
        }
        return $atts;
    }
}

header.blade.php


<header class="banner">

    @if (has_nav_menu('primary_navigation'))
    <nav class="navbar navbar-expand-lg navbar-light bg-light"
         aria-label="{{ wp_get_nav_menu_name('primary_navigation') }}">
        <div class="container">
            <a class="navbar-brand" href="{{ home_url('/') }}">
                {!! $siteName !!}
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main-nav"
                    aria-controls="main-nav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="main-nav">
                {!! wp_nav_menu([
                'theme_location' => 'primary_navigation',
                'menu_class' => 'navbar-nav me-auto mb-2 mb-lg-0',
                'walker' => new \App\BootstrapNav(),
                ]) !!}
            </div>
        </div>
    </nav>
    @endif
</header>

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.