Add Dropdown menu using “add_filter => wp_nav_menu_items”

I think this will do what you are looking to accomplish. Basically, this way you hook in before the HTML is generated to add your menu items. I haven’t had a chance to test this, but it should work.

EDIT: Since you want to have it underneath a dropdown, you need to set the menu_item_parent. If the user icon menu item is going to stay static, you can grab the nav menu item ID by looking at the source code for that menu item, and grabbing the number that follows menu-item- in the ID.

enter image description here

For example, in this case 103191 is the nav menu item ID (note that this is not the same as the object/post/page/CPT/etc a menu item points to).

function my_account_loginout_link( $items, $menu, $args ) {
    if ( $args->theme_location !== 'user' ) {
        return $items;
    }

    if ( is_user_logged_in() ) {
        $added_menu_items = array(
            array(
                'title' => 'Meus Dados',
                'url' => get_permalink( wc_get_page_id( 'myaccount' ) )
            ),
            array(
                'title' => 'Sair',
                'url' => wp_logout_url( get_home_url() )
            )
        );
    } else {
        $added_menu_items = array(
            array(
                'title' => 'Entrar',
                'url' => get_permalink( wc_get_page_id( 'myaccount' ) )
            )
        );
    }

    $parent_menu_id = 103191;
    $menu_order = count( $items ) + 1;
    foreach ( $added_menu_items as $added_item ) {
        $nav_menu_item = new stdClass;
        $nav_menu_item->menu_item_parent = $parent_menu_id;
        $nav_menu_item->url = $added_item['url'];
        $nav_menu_item->title = $added_item['title'];
        $nav_menu_item->menu_order = $menu_order;
        $items[] = $nav_menu_item;
        $menu_order++;
    }
    return $items;
}
add_filter( 'wp_get_nav_menu_items', 'my_account_loginout_link', 10, 3 );

Adapted from:

Leave a Comment