wp_nav_menu_container_allowedtags
Allows you to extend the list of HTML tags accepted in the 'container' parameter when a menu is output by wp_nav_menu(). Filters the list of menu container tags.
The menu container is specified in the container parameter of wp_nav_menu():
wp_nav_menu( array( 'container' => 'div', ) );
By default, only the div and nav tags can be specified. This filter allows the list to be extended.
Usage
add_filter( 'wp_nav_menu_container_allowedtags', 'wp_kama_nav_menu_container_allowedtags_filter' );
/**
* Function for `wp_nav_menu_container_allowedtags` filter-hook.
*
* @param string[] $tags The acceptable HTML tags for use as menu containers.
*
* @return string[]
*/
function wp_kama_nav_menu_container_allowedtags_filter( $tags ){
// filter...
return $tags;
}
- $tags(array)
- The allowed HTML tags for use as menu containers.
Default: an array containing 'div' and 'nav'
Examples
#1 Add the section tag to the allowed tag choices
// Add the section tag to the set of allowed tags
add_filter( 'wp_nav_menu_container_allowedtags', function ( $allowed_tags ) {
$allowed_tags[] = 'section';
return $allowed_tags;
} );
// Output the menu
wp_nav_menu( [
'theme_location' => 'mainmenu',
'container' => 'section',
] );
The result:
<section class="menu-mainmenu-container"> <ul id="menu-mainmenu" class="menu"> <li id="menu-item-268" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-268"> <a href="http://wp-test.ru/2018/03/25/post-105/">Post 1</a> </li> <li id="menu-item-269" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-269"> <a href="http://wp-test.ru/2018/03/25/post-104/">Post 2</a> </li> <li id="menu-item-270" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-270"> <a href="http://wp-test.ru/2018/03/25/post-103/">Post 3</a> </li> </ul> </section>
Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
wp_nav_menu_container_allowedtags
wp-includes/nav-menu-template.php 189
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );