wp_nav_menu() doesn’t apply correctly classes and IDs to menu and container

I believe this is caused probably by your theme or another plugin, filtering on wp_nav_menu_args and changing the items_wrap value. I tested your provided code and the output was correct, using blank WordPress 4.9.4 installation.

You could try passing items_wrap with default value:

wp_nav_menu( [
    'menu' => 'Primary Navigation',
    'menu_class' => 'this-is-the-menu-class',
    'menu_id' => 'this-is-the-menu-id',
    'container' => 'div',
    'container_class' => 'this-is-the-container-class',
    'container_id' => 'this-is-the-container-ID',
    'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>'
] );

But if something is filtering that out, it wouldn’t make a difference.

I recommend adding this to your child theme’s functions.php file, to remove all filters and see if that fixes it (to prove it is a filter causing your problem):

remove_all_filters( 'wp_nav_menu_args' );

You can also add this to your child theme’s functions.php, to dump out on screen, the $args array, where you can check the value of items_wrap:

add_filter( 'pre_wp_nav_menu', 'smyles_dump_nav_menu_args', 9999, 2 );

function smyles_dump_nav_menu_args( $null, $args ){
    ob_start();

    echo '<pre>';
    var_dump($args);
    echo '</pre>';

    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

Returning a true value (in our case, it’s HTML) to this filter will cause it to echo on page, when echo is passed true in the wp_nav_menu

Make sure to add echo and set true in wp_nav_menu call:

wp_nav_menu( [
    'menu' => 'Primary Navigation',
    'menu_class' => 'this-is-the-menu-class',
    'menu_id' => 'this-is-the-menu-id',
    'container' => 'div',
    'container_class' => 'this-is-the-container-class',
    'container_id' => 'this-is-the-container-ID',
    'echo' => true
] );

Leave a Comment

tech