Tweak Core Mobile Navigation Wrapper

PHP
function modify_navigation_block_output( $block_content, $block ) {
    if ( $block['blockName'] === 'core/navigation' ) {
        // 1. Create placeholder div for custom logo HTML
        $logo_placeholder = '<div id="custom-logo-placeholder" class="wp-block-navigation__responsive-logo">
            <!-- CUSTOM LOGO HTML GOES HERE -->
            ' . (get_custom_logo() ?: '<a href="' . esc_url( home_url( '/' ) ) . '" class="site-logo-text">' . get_bloginfo( 'name' ) . '</a>') . '
        </div>';
        
        // Insert logo placeholder before responsive-dialog
        $block_content = preg_replace(
            '/(<div class="wp-block-navigation__responsive-dialog[^>]*>)/',
            $logo_placeholder . '$1',
            $block_content,
            1
        );
        
        // 2. Create placeholder div for custom contact link HTML
        $contact_placeholder = '<div id="custom-contact-placeholder" class="wp-block-navigation__kontakt-link">
            <!-- CUSTOM CONTACT LINK HTML GOES HERE -->
            <a href="/kontakt">Kontakt</a>
        </div>';
        
        // Use DOM manipulation to ensure correct placement
        $dom = new DOMDocument();
        
        // The @ suppresses warnings from potentially malformed HTML
        @$dom->loadHTML(mb_convert_encoding($block_content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        
        // Find the responsive-dialog element
        $xpath = new DOMXPath($dom);
        $dialog_divs = $xpath->query('//div[contains(@class, "wp-block-navigation__responsive-dialog")]');
        
        if ($dialog_divs->length > 0) {
            $dialog_div = $dialog_divs->item(0);
            
            // Create the contact placeholder node
            $contact_fragment = $dom->createDocumentFragment();
            $contact_fragment->appendXML($contact_placeholder);
            
            // Insert after the dialog div (as a sibling, not a child)
            if ($dialog_div->parentNode) {
                $dialog_div->parentNode->insertBefore($contact_fragment, $dialog_div->nextSibling);
                
                // Get the modified HTML
                $block_content = $dom->saveHTML();
            }
        }
    }
    
    return $block_content;
}
add_filter( 'render_block', 'modify_navigation_block_output', 10, 2 );