Support » Developing with WordPress » wp_nav_menu_items custom item not saving title attribute

  • I am using wp_nav_menu_items to add an li element for a Woocommerce mini cart. However even though I’m explicitly setting the title attribute on the link to a string (View Quotee), it’s setting to whatever the text of the link is. So right now it’s just setting it to the number of products in the cart.

    function add_minicart_menu($menu, $args) {
            if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'top' !== $args->theme_location )
                return $menu;
    
            ob_start();
            
            global $woocommerce;
            
            $cart_url = $woocommerce->cart->get_cart_url();
            $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
            $cart_contents_count = $woocommerce->cart->cart_contents_count;
            $cart_contents = '<span class="count">'.$cart_contents_count.'</span>';
            $cart_items = $woocommerce->cart->get_cart();
            $menu_item = '<li><a class="minicart-contents" href="'. $cart_url .'" title="View Quote"><i class="fa fa-shopping-cart"></i>'.$cart_contents .'</a>';
            
            if ( $cart_contents_count > 0 ) {
                $menu_item .= '<ul class="mini-cart">';
                foreach($cart_items as $item)
                {
                    $menu_item .= '<li><a href="'.get_permalink($item->ID).'">'.get_the_title($item->ID).'</a></li>';
                }
                $menu_item .= '</ul>';
            }
            $menu_item .= '</li>';
                
            echo $menu_item;
            $nav = ob_get_clean();
            return $menu . $nav;
        }
        add_filter('wp_nav_menu_items','add_minicart_menu', 10, 2);

    When I error log $menu_item it is logging the right title. What is changing the title attribute in this element?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Menu item HTML is generated through a formatted print function. The format used does not include title attributes. WP had removed title attributes from generated links some time ago. If you want to have your menu item include a title attribute, you should add your item through the “wp_nav_menu” filter. The HTML you add here is immediately output without any more intervening code.

Viewing 1 replies (of 1 total)
  • The topic ‘wp_nav_menu_items custom item not saving title attribute’ is closed to new replies.