Programmatically Add Item to WP Multisite Menus

Does WP Multisite require a specific function in order to programmatically add a menu item to a menu?

A friend and I have a plugin that adds a menu item to your menu found here. When we created it we assumed it would work with WordPress Multisite, but the few users who have tried it have had difficulty.

Read More

One guy got a fatal error which doesn’t seem to be related: Fatal error: Call to undefined function wp_get_current_user() in /home/cmnet/public_html/wp-includes/capabilities.php on line 1451.

Another got it to show up, but said that it “crashed” I.E. and not all of the menu displayed.

Here’s how we’re currently doing this, but is there something we need to change to make this work with WordPress multisite.

Filter:

if ( isset( $this->options['menu_name_1'] ) && $this->options['menu_name_1'] != '0' ) {
                    add_filter( 'wp_nav_menu_' . $this->options['menu_name_1'] . '_items', array( &$this, 'add_itemcart_to_menu' ) , 10, 2 );
            }

Function:

public function add_itemcart_to_menu( $items ) {
            $classes = 'wpmenucart-display-'.$this->options['items_alignment'];

            if ($this->get_common_li_classes($items) != '')
                    $classes .= ' ' . $this->get_common_li_classes($items);

            $classes .= (isset($this->options['custom_class']) && $this->options['custom_class'] != '') ? sprintf( ' %s', $this->options['custom_class'] ) : '';

            // Filtering done here (instead of in function) to prevent issues with ajax
            $wpmenucart_menu_item = apply_filters( 'wpmenucart_menu_item_filter', $this->wpmenucart_menu_item() );

            $item_data = $this->shop->menu_item();

            if ($item_data['cart_contents_count'] > 0 || isset($this->options['always_display'])) {
                    $items .= '<li class="'.$classes.'">' . $wpmenucart_menu_item . '</li>';
            }

            return $items;
    }

Related posts