How to add “active” class to wp_nav_menu() current menu item (simple way)

I am creating custom WordPress theme using a starter theme _Underscores and Bootstrap.

I would like to modify wp_nav_menu so that it assigns the current menu item .active class instead of the default .current-menu-item. I need this in order to use .active class from Bootstrap.

Read More

Here is what I have (extra stuff comes from WP so please scroll to the right):

<ul id="menu-main-menu" class="nav navbar-nav">
   <li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-13 current_page_item menu-item-14"><a href="">item1</a></li>
   <li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-12"><a href="">item2</a></li>
</ul>

And here is what I need:

<ul id="menu-main-menu" class="nav navbar-nav">
   <li id="menu-item-14" class="active menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-13 current_page_item menu-item-14"><a href="">item1</a></li>
   <li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-12"><a href="">item2</a></li>
</ul>

I would prefer to achieve this without modifying ../wp-includes/nav-menu-template.php and without using JS.


UPDATE: I found the answer just before posting this question, but since I had a rather hard time finding it, posting this as a QA to hopefully save someone some time.

Related posts

Leave a Reply

5 comments

  1. To also highlight the menu item when one of the child pages is active, also check for the other class (current-page-ancestor) like below:

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    
    function special_nav_class ($classes, $item) {
        if (in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
            $classes[] = 'active ';
        }
        return $classes;
    }
    
  2. If you want the ‘active’ in the html:

    header with html and php:

            <?php
            $menu_items = wp_get_nav_menu_items( 'main_nav' ); // id or name of menu
            foreach ( (array) $menu_items as $key => $menu_item ) {
                if ( ! $menu_item->menu_item_parent ) {
                    echo "<li class=" . vince_check_active_menu($menu_item) . "><a href='$menu_item->url'>";
                    echo $menu_item->title;
                    echo "</a></li>";
                }
            }
            ?>
    

    functions.php:

    function vince_check_active_menu( $menu_item ) {
        $actual_link = ( isset( $_SERVER['HTTPS'] ) ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        if ( $actual_link == $menu_item->url ) {
            return 'active';
        }
        return '';
    }
    
  3. In addition to previous answers, if your menu items are Categories and you want to highlight them when navigating through posts, check also for current-post-ancestor:

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    
    function special_nav_class ($classes, $item) {
        if (in_array('current-post-ancestor', $classes) || in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
            $classes[] = 'active ';
        }
        return $classes;
    }
    
  4. In header.php insert this code to show menu:

    <?php
        wp_nav_menu(
            array(
                'theme_location' => 'menu-one',
                'walker' => new Custom_Walker_Nav_Menu_Top
            )
        );
    ?>
    

    In functions.php use this:

    class Custom_Walker_Nav_Menu_top extends Walker_Nav_Menu
    {
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
            $is_current_item = '';
            if(array_search('current-menu-item', $item->classes) != 0)
            {
                $is_current_item = ' class="active"';
            }
            echo '<li'.$is_current_item.'><a href="'.$item->url.'">'.$item->title;
        }
    
        function end_el( &$output, $item, $depth = 0, $args = array() ) {
            echo '</a></li>';
        }
    }