Create a MegaMenu using a walker and the start_lvl function

I’m trying to create a normal dropdown menu and a megamenu in WP by using a custom walker.

The problem that I have is that the usual ul tag needs to be replaced with a div.

I already created the an extra checkbox to enable the megamenu by using the
wp_update_nav_menu_item and wp_setup_nav_menu_item filters.

function custom_menu_fields( $menu_item ) { 
    $menu_item->is_mega_menu = get_post_meta( $menu_item->ID, '_menu_item_megamenu', true );
    return $menu_item;
add_filter('wp_setup_nav_menu_item', 'custom_menu_fields');

function fields_update($menu_id, $menu_item_db_id, $args) {         
    if ( isset($_REQUEST['menu-item-megamenu']) && is_array($_REQUEST['menu-item-megamenu'])) {
        $custom_value = $_REQUEST['menu-item-megamenu'][$menu_item_db_id];
        update_post_meta($menu_item_db_id, '_menu_item_megamenu', $custom_value);
add_action('wp_update_nav_menu_item', 'fields_update', 10, 3);

Then my walker:

public function start_lvl( &$output, $depth = 0, $args = array() ) {

        $indent     = str_repeat("t", $depth);     

        if( $this->is_mega_menu && $depth == 0 ) { 
             $indent = "t$indent";
             $output.= "$indent <div class="mega-menu">n";
        } else {
            $output.= "n$indent<ul class="dropdown-menu">n";

The problem is $this->is_mega_menu. Normally I would use $item->is_mega_menu or $item->classes but both won’t work inside the start_lvl public funtion
due to the "...should be compatible with Walker::start_lvl(&$output, $depth = 0, $args = Array)" the moment I add $item.

How to I connect $this->is_mega_menu with the custom checkbox to enable the megamenu?
Any help is much appreciated.

Read more here: Create a MegaMenu using a walker and the start_lvl function

Leave a Reply

Your email address will not be published. Required fields are marked *