Skip to content

Commit 8ddb536

Browse files
authored
Merge pull request #339 from wp-bootstrap/v4
Make v4 branch the new master
2 parents 88959a7 + 1773f72 commit 8ddb536

File tree

8 files changed

+872
-298
lines changed

8 files changed

+872
-298
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
#CHANGELOG
22

3+
## [4.0.0]
4+
- Added a prefix on @since tags to highlight when they refer to WP core instead of this class.
5+
- Rework of `start_lvl()` and `start_el()` based on latest `Walker_Nav_Menu` Class from WP core.
6+
- Whitespace preservation method improvements.
7+
- Added `nav_menu_item_args` filter and `nav_menu_item_title` brought in at WP 4.4.0
8+
- Use `the_title` filter prior to `nav_menu_item_title`.
9+
- Added a labelled-by attribute to dropdowns for accessibility.
10+
- Links inside dropdown have `.dropdown-item` instead of `.nav-link`.
11+
- Remove `<span class="carat">` after parent dropdown items.
12+
- Support `echo` arg in fallback menu. props: @toddlevy
13+
- Add `.active` to parent when a child is current page. props: @zyberspace
14+
- Fix to correct output of dropdown atts and styles when depth passed to wp_nav_menu is <= 1. props: @chrisgeary92
15+
- Move icon output to a local var instead of modifying and clearing a global object.
16+
- Reassign filtered classes back to $classes array so that updated classes can be accessed later if needed. props: @lf-jeremy
17+
- Update to work with Bootstrap v4.
18+
- Added `.nav-item` and `.nav-link` to `<li>` and `<a>` respectively.
19+
- Dropped support for using title attribute to add link modifiers and icons.
20+
- Added support for link modifiers and icons through WP Menu Builder 'classes' input.
21+
- Icon support is for Font Awesome 4/5 and Glyphicons icons.
22+
- Added unit tests for the `fallback` method.
23+
- Added code to handle icon-only menus.
24+
325
## [3.0.0]
426

527
- Fix untranslated string in fallback.

README.md

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,86 @@
88
[![Code Coverage](https://scrutinizer-ci.com/g/wp-bootstrap/wp-bootstrap-navwalker/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/wp-bootstrap/wp-bootstrap-navwalker/?branch=master)
99
[![Build Status](https://scrutinizer-ci.com/g/wp-bootstrap/wp-bootstrap-navwalker/badges/build.png?b=master)](https://scrutinizer-ci.com/g/wp-bootstrap/wp-bootstrap-navwalker/build-status/master)
1010

11-
A custom WordPress nav walker class to fully implement the Bootstrap 3.0+ navigation style in a custom theme using the WordPress built in menu manager. A working version of the walker for Bootstrap 4.0.0 can be found in the [`v4` branch](https://github.com/wp-bootstrap/wp-bootstrap-navwalker/tree/v4)
11+
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.
1212

1313
## NOTES
1414

15-
This is a utility class that is intended to format your WordPress theme menu with the correct syntax and classes to utilize the Bootstrap dropdown navigation. It does not include the required Bootstrap JS and CSS files. You will have to include those dependancies separately.
15+
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.
1616

17-
### Bootstrap 4
17+
### UPGRADE NOTES ###
1818

19-
Bootstrap 4.0.0 released January 2018 and is the default branch offered at the GitHub repo and on [GetBootstrap](https://getbootstrap.com).
19+
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
20+
21+
Here is a list of the most notable changes:
22+
23+
- The filename has been changed and prefixed with `class-` to better fit PHP coding standards naming conventions.
24+
- New Name: `class-wp-bootstrap-navwalker.php`
25+
- Old Name: `wp-bootstrap-navwalker.php`
26+
- Icon and link modifier handling is now done through the `CSS Classes` menu item input instead of the `Title` input.
27+
- Icon only items are possible using icon classes in combination with the `sr-only` classname.
2028

2129
## Installation
2230

23-
Place **wp-bootstrap-navwalker.php** in your WordPress theme folder `/wp-content/your-theme/`
31+
Place **class-wp-bootstrap-navwalker.php** in your WordPress theme folder `/wp-content/your-theme/`
2432

25-
Open your WordPress themes **functions.php** file `/wp-content/your-theme/functions.php` and add the following code:
33+
Open your WordPress themes **functions.php** file - `/wp-content/your-theme/functions.php` - and add the following code:
2634

2735
```php
2836
<?php
2937
// Register Custom Navigation Walker
30-
require_once get_template_directory() . '/wp-bootstrap-navwalker.php';
38+
require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
3139
```
3240

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

3543
```php
3644
<?php
37-
if ( ! file_exists( get_template_directory() . '/wp-bootstrap-navwalker.php' ) ) {
45+
if ( ! file_exists( get_template_directory() . '/class-wp-bootstrap-navwalker.php' ) ) {
3846
// file does not exist... return an error.
39-
return new WP_Error( 'wp-bootstrap-navwalker-missing', __( 'It appears the wp-bootstrap-navwalker.php file may be missing.', 'wp-bootstrap-navwalker' ) );
47+
return new WP_Error( 'class-wp-bootstrap-navwalker-missing', __( 'It appears the class-wp-bootstrap-navwalker.php file may be missing.', 'wp-bootstrap-navwalker' ) );
4048
} else {
4149
// file exists... require it.
42-
require_once get_template_directory() . '/wp-bootstrap-navwalker.php';
50+
require_once get_template_directory . '/class-wp-bootstrap-navwalker.php';
4351
}
4452
```
53+
You will also need to declare a new menu in your `functions.php` file if one doesn't already exist.
4554

55+
```php
56+
register_nav_menus( array(
57+
'primary' => __( 'Primary Menu', 'THEMENAME' ),
58+
) );
59+
```
4660
## Usage
4761

48-
Update your `wp_nav_menu()` function in `header.php` to use the new walker by adding a "walker" item to the wp_nav_menu array.
62+
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.
4963

5064
```php
5165
<?php
5266
wp_nav_menu( array(
53-
'theme_location' => 'primary',
54-
'depth' => 2,
55-
'container' => 'div',
56-
'container_class' => 'collapse navbar-collapse',
57-
'container_id' => 'bs-example-navbar-collapse-1',
58-
'menu_class' => 'nav navbar-nav',
59-
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
60-
'walker' => new WP_Bootstrap_Navwalker(),
67+
'theme_location' => 'primary',
68+
'depth' => 1, // 1 = with dropdowns, 0 = no dropdowns.
69+
'container' => 'div',
70+
'container_class' => 'collapse navbar-collapse',
71+
'container_id' => 'bs-example-navbar-collapse-1',
72+
'menu_class' => 'navbar-nav mr-auto',
73+
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
74+
'walker' => new WP_Bootstrap_Navwalker()
6175
) );
76+
?>
6277
```
6378

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

66-
You will also want to declare your new menu in your `functions.php` file if one is not already defined.
81+
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.
6782

6883
```php
69-
<?php
70-
register_nav_menus( array(
71-
'primary' => __( 'Primary Menu', 'THEMENAME' ),
72-
) );
73-
```
74-
75-
Typically the menu is wrapped with additional markup, here is an example of a ` navbar-fixed-top` menu that collapse for responsive navigation.
76-
77-
```php
78-
<nav class="navbar navbar-default" role="navigation">
79-
<div class="container-fluid">
80-
<!-- Brand and toggle get grouped for better mobile display -->
81-
<div class="navbar-header">
82-
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
83-
<span class="sr-only">Toggle navigation</span>
84-
<span class="icon-bar"></span>
85-
<span class="icon-bar"></span>
86-
<span class="icon-bar"></span>
87-
</button>
88-
<a class="navbar-brand" href="<?php echo home_url(); ?>">
89-
<?php bloginfo('name'); ?>
90-
</a>
91-
</div>
84+
<nav class="navbar navbar-expand-md navbar-light bg-light" role="navigation">
85+
<div class="container">
86+
<!-- Brand and toggle get grouped for better mobile display -->
87+
<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="Toggle navigation">
88+
<span class="navbar-toggler-icon"></span>
89+
</button>
90+
<a class="navbar-brand" href="#">Navbar</a>
9291
<?php
9392
wp_nav_menu( array(
9493
'theme_location' => 'primary',
@@ -98,8 +97,8 @@ Typically the menu is wrapped with additional markup, here is an example of a `
9897
'container_id' => 'bs-example-navbar-collapse-1',
9998
'menu_class' => 'nav navbar-nav',
10099
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
101-
'walker' => new WP_Bootstrap_Navwalker())
102-
);
100+
'walker' => new WP_Bootstrap_Navwalker()
101+
) );
103102
?>
104103
</div>
105104
</nav>
@@ -129,25 +128,39 @@ add_filter( 'wp_nav_menu_args', 'prefix_modify_nav_menu_args' );
129128
```
130129
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.
131130

132-
### Extras
131+
### Menu Caching
132+
133+
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.
134+
135+
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.
133136

134-
This script included the ability to add Bootstrap dividers, dropdown headers, glyphicons and disabled links to your menus through the WordPress menu UI.
137+
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/
135138

136-
#### Dividers
139+
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.
137140

138-
Simply add a Link menu item with a **URL** of `#` and a **Link Text** or **Title Attribute** of `divider` (case-insensitive so ‘divider’ or ‘Divider’ will both work ) and the class will do the rest.
141+
See also:
142+
- https://generatewp.com/how-to-use-transients-to-speed-up-wordpress-menus/
143+
- https://vip-svn.wordpress.com/plugins/cache-nav-menu/cache-nav-menu.php
139144

140-
#### Glyphicons
145+
### Extras
146+
147+
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).
148+
149+
#### Icons
141150

142-
To add an Icon to your link simple place the Glyphicon class name in the links **Title Attribute** field and the class will do the rest. IE `glyphicon-bullhorn`
151+
To add an Icon to your link simply enter Glypicons 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`.
143152

144-
#### Dropdown Headers
153+
#### Icon-Only Items
145154

146-
Adding a dropdown header is very similar, add a new link with a **URL** of `#` and a **Title Attribute** of `dropdown-header` (it matches the Bootstrap CSS class so it's easy to remember). set the **Navigation Label** to your header text and the class will do the rest.
155+
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.
147156

148157
#### Disabled Links
149158

150-
To set a disabled link simply set the **Title Attribute** to `disabled` and the class will do the rest.
159+
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 followable link._
160+
161+
#### Dropdown Headers & Dropdown Dividers
162+
163+
Headers and dividers can be added within dropdowns by adding a Custom Link and adding either `dropdown-header` or `dropdown-divider` 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._
151164

152165
## Changelog
153166

0 commit comments

Comments
 (0)