My project: WordPress, Woocommerce, Bootstrap 4, bs4navwalker

My problem: Custom dropdown item added by WP filter

I’m using a filter (add_filter( ‘wp_nav_menu_items’,) to add login, logout and myaccount details in my navbar, everything is ok, but i’d like to add these itens inside an dropdown. The filter just creat one or two more itens to the ‘user’ menu.

I understand the is it happening because i’m adding an

  • , but I couldn’t figure out how to add all the code bootstrap needs to manage the dropdown.

    Is it possible?

    add_filter CODE

    add_filter( 'wp_nav_menu_items', 'my_account_loginout_link', 10, 2 );
    /**
     * Add WooCommerce My Account Login/Logout to Menu
     * 
     * @see https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area
     */
    function my_account_loginout_link( $items, $args ) {
        if (is_user_logged_in() && $args->theme_location == 'user') { //change your theme location menu to suit
    
    
            $items .= '                 
                        <li><a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Meus Dados</a></li>
                        <li><a class="nav-link" href="'. wp_logout_url( get_home_url() ) .'">Sair</a></li>
                        '; //change logout link, here it goes to 'shop', you may want to put it to 'myaccount'
        }
        elseif (!is_user_logged_in()  && $args->theme_location == 'user') {//change your theme location menu to suit
    
    
    
            $items .= '<li><a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Entrar</a></li>';
        }
        return $items;
    }
    

    User Menu CODE

        <!-- User Menu -->
        <?php 
          wp_nav_menu([
           'menu'            => 'user',
           'theme_location'  => 'user',
           'container'       => 'div',
           'container_id'    => 'bs4navbar',
           //'container_class' => 'collapse navbar-collapse',
           'menu_id'         => false,
           'menu_class'      => 'navbar-nav mr-auto',
           'depth'           => 0,
           'fallback_cb'     => 'bs4navwalker::fallback',
           'walker'          => new bs4navwalker()
         ]);
        ?>
    

    Read more here: Add Dropdown menu using "add_filter => wp_nav_menu_items"


    Solution:

    If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


    Wordpress related questions and answers: