All Projects → wp-bootstrap → Wp Bootstrap Navwalker

wp-bootstrap / Wp Bootstrap Navwalker

Licence: gpl-3.0
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Wp Bootstrap Navwalker

Webstack
WordPress 版 WebStack 导航主题 https://nav.iowen.cn
Stars: ✭ 662 (-79.88%)
Mutual labels:  wordpress, wordpress-theme, navigation, nav
Wordpress Bootstrap
Bootstrap in WordPress theme form - Bootstrap 3.3.1
Stars: ✭ 1,494 (-54.59%)
Mutual labels:  wordpress, wordpress-theme, bootstrap
Startwordpress
Learn how to develop a WordPress theme from scratch with this basic skeleton and accompanying tutorial.
Stars: ✭ 187 (-94.32%)
Mutual labels:  wordpress, wordpress-theme, bootstrap
wp-bootstrap4-navwalker
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.x navigation style in a custom theme using the WordPress built in menu manager
Stars: ✭ 40 (-98.78%)
Mutual labels:  wordpress-theme, walker, wp-bootstrap-navwalker
Wordpress Theme Puock
🎨 一款基于WordPress开发的高颜值的自适应主题,支持白天与黑夜模式、无刷新加载等。
Stars: ✭ 394 (-88.02%)
Mutual labels:  wordpress, wordpress-theme, bootstrap
Sage
WordPress starter theme with a modern development workflow
Stars: ✭ 11,531 (+250.49%)
Mutual labels:  wordpress, wordpress-theme, bootstrap
Air Light
WordPress starter theme - designed to be minimal, lightweight and easy for all kinds of WordPress projects. Public Roadmap: https://favro.com/organization/3b45e73eaf083f68fefef368/c1dd2d4a99d6723904d2e763
Stars: ✭ 285 (-91.34%)
Mutual labels:  wordpress, wordpress-theme, navigation
Go
The most flexible Gutenberg-first WordPress theme built for go-getters everywhere.
Stars: ✭ 218 (-93.37%)
Mutual labels:  wordpress, wordpress-theme
Vuetheme
WordPress theme using Rest API and Vue.js
Stars: ✭ 219 (-93.34%)
Mutual labels:  wordpress, wordpress-theme
Astra
A very lightweight and beautiful theme made to work with Page Builders.
Stars: ✭ 252 (-92.34%)
Mutual labels:  wordpress, wordpress-theme
vue-bottom-navigation
Vue bottom navigation
Stars: ✭ 56 (-98.3%)
Mutual labels:  navigation, menus
bsnav
An extended Bootstrap 4 menu with a bunch of utilities
Stars: ✭ 90 (-97.26%)
Mutual labels:  navigation, nav
ml-stack-nav
Customizable, responsive, accessible, easy-to-use multi-level stack navigation menu with slide effect.
Stars: ✭ 20 (-99.39%)
Mutual labels:  navigation, menus
Wp Tailwindcss Theme Boilerplate
A minimalist boilerplate for WordPress theme development using Tailwind CSS, SCSS, and Laravel Mix.
Stars: ✭ 199 (-93.95%)
Mutual labels:  wordpress, wordpress-theme
Create React Wptheme
Create modern, React-enabled WordPress themes with a single command.
Stars: ✭ 252 (-92.34%)
Mutual labels:  wordpress, wordpress-theme
Wpthemereview
PHP_CodeSniffer rules (sniffs) to enforce WordPress theme review coding conventions
Stars: ✭ 173 (-94.74%)
Mutual labels:  wordpress, wordpress-theme
Cornerstone
Cornerstone is a WordPress starter theme based on the Zurb Foundation Responsive Framework. Cornerstone aims to provide a lightweight starter theme that is responsive and SEO friendly that web designers can build great looking websites on.
Stars: ✭ 154 (-95.32%)
Mutual labels:  wordpress, wordpress-theme
E107
e107 Bootstrap CMS (Content Management System) v2 with PHP, MySQL, HTML5, jQuery and Twitter Bootstrap. Issue Discussion Room: https://gitter.im/e107inc/e107
Stars: ✭ 272 (-91.73%)
Mutual labels:  bootstrap, twitter-bootstrap
Lumberjack
Lumberjack is a powerful MVC framework for the modern WordPress developer. Write better, more expressive and easier to maintain code.
Stars: ✭ 261 (-92.07%)
Mutual labels:  wordpress, wordpress-theme
Menuspy
A JavaScript library to make navigation menus highlight the item based on currently in view section.
Stars: ✭ 283 (-91.4%)
Mutual labels:  navigation, nav

WP Bootstrap Navwalker

Code Climate Test Coverage Issue Count Build Status Scrutinizer Code Quality Code Coverage Build Status

This code in the main repo branch is undergoing a big shakeup to bring it in line with recent standards and to merge and test the backlog of PRs I have left for too long. Please use the v4.3.0 tag for stable version while this process happens. https://github.com/wp-bootstrap/wp-bootstrap-navwalker/releases/tag/v4.3.0

A custom WordPress Nav Walker Class to fully implement the Bootstrap 4 navigation style in a custom theme using the WordPress built in menu manager.

NOTES

This is a utility class that is intended to format your WordPress theme menu with the correct syntax and CSS classes to utilize the Bootstrap dropdown navigation. It does not include the required Bootstrap JS and CSS files - you will have to include them manually.

WordPress.org Theme Compliance

This walker is fully compliant with all Theme Review guidelines for wordpress.org theme submission. It requires no modification to be compliant but you can optionally replace the wp-bootstrap-navwalker text domain (which appears twice in the fallback function) with the text domain of your theme.

Upgrade Notes

Between version 3 and version 4 of the walker there have been significant changes to the codebase. Version 4 of the walker is built to work with Bootstrap 4 and has not been tested for backwards compatibility with Bootstrap 3. A separate branch for Bootstrap 3 is maintained here: https://github.com/wp-bootstrap/wp-bootstrap-navwalker/tree/v3-branch

Here is a list of the most notable changes:

  • The filename has been changed and prefixed with class- to better fit PHP coding standards naming conventions.
    • New Name: class-wp-bootstrap-navwalker.php
    • Old Name: wp-bootstrap-navwalker.php
  • Icon and link modifier handling is now done through the CSS Classes menu item input instead of the Title input.
  • Icon only items are possible using icon classes in combination with the sr-only classname.

Installation

Place class-wp-bootstrap-navwalker.php in your WordPress theme folder /wp-content/themes/your-theme/

Open your WordPress themes functions.php file - /wp-content/themes/your-theme/functions.php - and add the following code:

/**
 * Register Custom Navigation Walker
 */
function register_navwalker(){
	require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
}
add_action( 'after_setup_theme', 'register_navwalker' );

If you encounter errors with the above code use a check like this to return clean errors to help diagnose the problem.

if ( ! file_exists( get_template_directory() . '/class-wp-bootstrap-navwalker.php' ) ) {
    // File does not exist... return an error.
    return new WP_Error( 'class-wp-bootstrap-navwalker-missing', __( 'It appears the class-wp-bootstrap-navwalker.php file may be missing.', 'wp-bootstrap-navwalker' ) );
} else {
    // File exists... require it.
    require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
}

You will also need to declare a new menu in your functions.php file if one doesn't already exist.

register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'THEMENAME' ),
) );

Usage

Add or update any wp_nav_menu() functions in your theme (often found in header.php) to use the new walker by adding a 'walker' item to the wp_nav_menu args array.

wp_nav_menu( array(
    'theme_location'  => 'primary',
    'depth'           => 2, // 1 = no dropdowns, 2 = with dropdowns.
    'container'       => 'div',
    'container_class' => 'collapse navbar-collapse',
    'container_id'    => 'bs-example-navbar-collapse-1',
    'menu_class'      => 'navbar-nav mr-auto',
    'fallback_cb'     => 'WP_Bootstrap_Navwalker::fallback',
    'walker'          => new WP_Bootstrap_Navwalker(),
) );

Your menu will now be formatted with the correct syntax and classes to implement Bootstrap dropdown navigation.

Typically the menu is wrapped with additional markup, here is an example of a fixed-top menu that collapse for responsive navigation at the md breakpoint.

<nav class="navbar navbar-expand-md navbar-light bg-light" role="navigation">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-controls="bs-example-navbar-collapse-1" aria-expanded="false" aria-label="<?php esc_attr_e( 'Toggle navigation', 'your-theme-slug' ); ?>">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Navbar</a>
        <?php
        wp_nav_menu( array(
            'theme_location'    => 'primary',
            'depth'             => 2,
            'container'         => 'div',
            'container_class'   => 'collapse navbar-collapse',
            'container_id'      => 'bs-example-navbar-collapse-1',
            'menu_class'        => 'nav navbar-nav',
            'fallback_cb'       => 'WP_Bootstrap_Navwalker::fallback',
            'walker'            => new WP_Bootstrap_Navwalker(),
        ) );
        ?>
    </div>
</nav>

To change your menu style add Bootstrap nav class names to the menu_class declaration.

Review options in the Bootstrap docs for more information on nav classes.

Displaying the Menu

To display the menu you must associate your menu with your theme location. You can do this by selecting your theme location in the Theme Locations list while editing a menu in the WordPress menu manager.

Making this Walker the Default Walker for Nav Menus

There has been some interest in making this walker the default walker for all menus. That could result in some unexpected situations but it can be achieved by adding this function to your functions.php file.

function prefix_modify_nav_menu_args( $args ) {
    return array_merge( $args, array(
        'walker' => new WP_Bootstrap_Navwalker(),
    ) );
}
add_filter( 'wp_nav_menu_args', 'prefix_modify_nav_menu_args' );

Simply updating the walker may not be enough to get menus working right, you may need to add wrappers or additional classes, you can do that via the above function as well.

Usage with Bootstrap 5

Bootstrap 5 uses namespaced data attributes. All data attributes now include bs as an infix. The new attributes work just like the old ones. Here’s the menu toggle button from the example above with the renamed data attributes.

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#bs-example-navbar-collapse-1" aria-controls="bs-example-navbar-collapse-1" aria-expanded="false" aria-label="<?php esc_attr_e( 'Toggle navigation', 'your-theme-slug' ); ?>">
    <span class="navbar-toggler-icon"></span>
</button>

The walker also adds a data attribute for dropdown toggles via the start_el() method. Paste this to your functions.php to make the walker use the infixed data attibute.

add_filter( 'nav_menu_link_attributes', 'prefix_bs5_dropdown_data_attribute', 20, 3 );
/**
 * Use namespaced data attribute for Bootstrap's dropdown toggles.
 *
 * @param array    $atts HTML attributes applied to the item's `<a>` element.
 * @param WP_Post  $item The current menu item.
 * @param stdClass $args An object of wp_nav_menu() arguments.
 * @return array
 */
function prefix_bs5_dropdown_data_attribute( $atts, $item, $args ) {
    if ( is_a( $args->walker, 'WP_Bootstrap_Navwalker' ) ) {
        if ( array_key_exists( 'data-toggle', $atts ) ) {
            unset( $atts['data-toggle'] );
            $atts['data-bs-toggle'] = 'dropdown';
        }
    }
    return $atts;
}

Menu Caching

On some sites generating a large menu that rarely ever changes on every page request is an overhead that you may want to avoid. In those cases I can suggest you look at storing menu results in a transient.

The biggest drawback to caching nav menus with this method is that it cannot easily apply the .current-menu-item or the .active class to the currently active item as WP decides what is currently active on page load - and since the menu is cached it only knows the active page that it was cached on originally.

You can decide yourself if you want to put up with those drawbacks for the benefit of removing the menu generation time on most page loads. You can follow this article by Dave Clements to see how we cached nav menus that were generated by this walker: https://www.doitwithwp.com/use-transients-speed-wordpress-menus/

Be sure to set the echo argument to FALSE in the wp_nav_menu() call when doing this so that the results can be stored instead of echoed to the page.

See also:

Extras

This script included the ability to use Bootstrap nav link mods in your menus through the WordPress menu UI. Disabled links, dropdown headers and dropdown dividers are supported. Additionally icon support is built-in for Glyphicons and Font Awesome (note: you will need to include the icon stylesheets or assets separately).

Icons

To add an Icon to your link simply enter Glyphicons or Font Awesome class names in the links CSS Classes field in the Menu UI and the walker class will do the rest. IE glyphicons glyphicons-bullhorn or fa fa-arrow-left or fas fa-arrow-left.

Icon-Only Items

To make an item appear with the icon only apply the bootstrap screen reader class sr-only to the item alongside any icon classnames. This will then hide only the text that would appear as the link text.

Disabled Links

To set a disabled link simply add disabled to the CSS Classes field in the Menu UI and the walker class will do the rest. Note: In addition to adding the .disabled class this will change the link href to # as well so that it is not a follow-able link.

Dropdown Headers, Dropdown Dividers & Dropdown Item Text

Headers, dividers and text only items can be added within dropdowns by adding a Custom Link and adding either dropdown-header, dropdown-divider or dropdown-item-text into the CSS Classes input. Note: This will remove the href on the item and change it to either a <span> for headers or a <div> for dividers.

Missing Edit Shortcut in Customizer Preview

According to the documentation for wp_nav_menu() one has to provide an instance of the custom walker class in order to apply the custom walker to the menu. As the instance is not JSON serializable this will cause the menu edit shortcut to not appear in the Customizer preview. To fix this do the following:

  1. Provide the class name string instead of the class instance as value for the 'walker' key in the array of wp_nav_menu's arguments,
wp_nav_menu( array(
    'theme_location'  => 'primary',
    'depth'           => 2, // 1 = no dropdowns, 2 = with dropdowns.
    'container'       => 'div',
    'container_class' => 'collapse navbar-collapse',
    'container_id'    => 'bs-example-navbar-collapse-1',
    'menu_class'      => 'navbar-nav mr-auto',
    'fallback_cb'     => 'WP_Bootstrap_Navwalker::fallback',
-    'walker'          => new WP_Bootstrap_Navwalker(),
+    'walker'          => 'WP_Bootstrap_Navwalker',
) );
  1. re-add the class instance by adding this filter to your functions.php
function slug_provide_walker_instance( $args ) {
    if ( isset( $args['walker'] ) && is_string( $args['walker'] ) && class_exists( $args['walker'] ) ) {
        $args['walker'] = new $args['walker'];
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'slug_provide_walker_instance', 1001 );

Changelog

Please see the Changelog.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].