How to dynamically add custom taxonomy terms as a sub-menu of an existing menu item, using custom walker class

Okay, I figured it out. It’s not very smooth, but at least it works as I want it to. Will have to do some refactoring.

The issue was that wp_nav_menu() got called earlier than the taxonomy registration. I had accidentally placed it in my theme-functions.php and forgotten about it, then called it again in my header.php . That’s why it seemed to work, but I still got the invalid taxonomy error and a broken menu.

Here’s the final walker class:

class Walker_Add_Myterms extends Walker_Nav_Menu {
    public function end_el( &$output, $item, $depth=1, $args=array() ) {
    // if the current menu item being output has class "uthyrning"
    if( in_array( 'parentliitemclass' , $item->classes ) ){
        // get all terms
        $myterms = get_terms( array(
            'taxonomy'      => 'mytax',
            'hide_empty'    => true,
            'parent'        => 0,
            ) );
        if ( ! empty( $myterms ) && ! is_wp_error( $myterms ) ) {
            // Insert a submenu
            $output .= '<ul class="sub-menu">';
            // iterate over each type and add an li
            foreach( $myterms as $myterm){

                $term_url   = get_term_link( $myterm->term_id , 'mytax' );
                $name       = $myterm->name;
                $li_id      = 'id="term-item-' . $myterm->term_id . '"';
                $li_classes="menu-item menu-item-type-taxonomy menu-item-object-mytax term-item-" . $myterm->term_id;
                $wrap_class="class="ancestor-wrapper"";
                $format="<li %s class="https://wordpress.stackexchange.com/questions/353699/%s"><div %s><a href="https://wordpress.stackexchange.com/questions/353699/%s">%s</a></div></li>";

                // Get child terms ,and if they exist, create sub-menu with term children

                $children = get_term_children( $myterm->term_id, 'mytax' );
                if( isset($children) && ! is_wp_error( $children ) && sizeof($children) > 0) {
                    $li_classes .= ' menu-item-has-children';
                    $child_items="";
                    foreach ( $children as $child ) {
                        $child_term = get_term( $child, 'mytax' );
                        $child_item     =   '<li id="term-item-' . $child_term->term_id . '" class="menu-item menu-item-type-taxonomy menu-item-object-mytax term-item-' . $child_term->term_id . '"><div class="ancestor-wrapper">';
                        $child_item     .=  '<a href="' . get_term_link( $child_term, 'mytax' ) . '">' . $child_term->name . '</a>';
                        $child_item     .=  '</div></li>';
                        $child_items    .=  $child_item;
                    }
                    $format="<li %s class="https://wordpress.stackexchange.com/questions/353699/%s"><div %s><a href="https://wordpress.stackexchange.com/questions/353699/%s">%s</a>";
                    $format         .= '<button class="toggle sub-menu-toggle fill-children-current-color" data-toggle-target=".menu-modal .term-item-' . $myterm->term_id . ' > .sub-menu" data-toggle-type="slidetoggle" data-toggle-duration="250" aria-expanded="false"><span class="screen-reader-text">Visa undermeny</span><svg class="svg-icon" aria-hidden="true" role="img" focusable="false" xmlns="http://www.w3.org/2000/svg" width="20" height="12" viewBox="0 0 20 12"><polygon fill="" fill-rule="evenodd" points="1319.899 365.778 1327.678 358 1329.799 360.121 1319.899 370.021 1310 360.121 1312.121 358" transform="translate(-1310 -358)"></polygon></svg></button>';
                    $format         .= '</div><ul class="sub-menu">';
                    $format         .= $child_items . '</ul></li>';
                }

                // END Child terms
                $output     .= sprintf( $format, $li_id, $li_classes, $wrap_class, $term_url, $name );
            }
            // close the list
            $output .= '</ul>';
        }
    }
    // close the parent li
    $output .= "</li>\n";  
    }
}

And I also ended up copying the template-part where the wp_nav_menu() is called, from the parent theme, and replacing the default walker_nav_menu class with my own extended class above.