Skip to content

Commit 4bdec67

Browse files
authored
Merge pull request #516 from wp-bootstrap/IanDelMar/BS5-readme
Add instructions for usage with Bootstrap 5
2 parents c856e94 + 55b979a commit 4bdec67

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ add_filter( 'wp_nav_menu_args', 'prefix_modify_nav_menu_args' );
136136

137137
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.
138138

139+
### Usage with Bootstrap 5
140+
141+
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.
142+
143+
```php
144+
<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' ); ?>">
145+
<span class="navbar-toggler-icon"></span>
146+
</button>
147+
```
148+
149+
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.
150+
151+
```php
152+
add_filter( 'nav_menu_link_attributes', 'prefix_bs5_dropdown_data_attribute', 20, 3 );
153+
/**
154+
* Use namespaced data attribute for Bootstrap's dropdown toggles.
155+
*
156+
* @param array $atts HTML attributes applied to the item's `<a>` element.
157+
* @param WP_Post $item The current menu item.
158+
* @param stdClass $args An object of wp_nav_menu() arguments.
159+
* @return array
160+
*/
161+
function prefix_bs5_dropdown_data_attribute( $atts, $item, $args ) {
162+
if ( is_a( $args->walker, 'WP_Bootstrap_Navwalker' ) ) {
163+
if ( array_key_exists( 'data-toggle', $atts ) ) {
164+
unset( $atts['data-toggle'] );
165+
$atts['data-bs-toggle'] = 'dropdown';
166+
}
167+
}
168+
return $atts;
169+
}
170+
```
171+
139172
### Menu Caching
140173

141174
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.

0 commit comments

Comments
 (0)