How to change default meaning of semantic tag

This topic has 4 replies, 3 voices, and was last updated 1 minute ago ago by Tony Rodriguez

  • Avatar: Rok
    Rok
    Participant
    January 28, 2026 at 07:42

    Hi team,
    I faced an issue when I added categories to the menu dynamically. Added this code in function.php:

    
    /**
     * SHS: Mega Menu categories hover + spacing fix (XStore / Elementor)
     * Works both:
     *  - when opening the mega menu template directly
     *  - when the same template is rendered inside the header menu
     */
    add_filter('elementor/frontend/the_content', function ($content) {
    
        // Run only when that specific block exists
        if (stripos($content, 'categories-mask') === false) {
            return $content;
        }
    
        /**
         * A) Ensure the <a> inside categories-mask has a predictable class
         *    (so we can target it with high-specificity CSS)
         */
        $content = preg_replace_callback(
            '#<div([^>]*class="[^"]*categories-mask[^"]*"[^>]*)>(\s*)<a([^>]*)>#i',
            function ($m) {
                $divOpen = '<div' . $m[1] . '>' . $m[2];
                $aAttrs  = $m[3];
    
                if (preg_match('/\sclass="([^"]*)"/i', $aAttrs, $cm)) {
                    $classes = trim($cm[1]);
    
                    if (stripos($classes, 'shs-mega-link') === false) {
                        $classes .= ' shs-mega-link';
                    }
    
                    $aAttrs = preg_replace(
                        '/\sclass="[^"]*"/i',
                        ' class="' . esc_attr($classes) . '"',
                        $aAttrs,
                        1
                    );
                } else {
                    $aAttrs .= ' class="shs-mega-link"';
                }
    
                return $divOpen . '<a' . $aAttrs . '>';
            },
            $content
        );
    
        /**
         * B) Normalize headings inside categories-mask (remove h4 default spacing)
         *    Keep structure, just remove browser/theme margins that break alignment.
         */
        // Add a helper class to h4 if present
        $content = preg_replace(
            '#(<div[^>]*class="[^"]*categories-mask[^"]*"[^>]*>.*?<a[^>]*>)(\s*)<h4\b([^>]*)>#is',
            '$1$2<h4 class="shs-mega-link-text"$3>',
            $content
        );
    
        /**
         * C) Inject CSS once (critical: strong specificity + !important)
         *    Targets only the mega menu area.
         */
        static $css_done = false;
        if (!$css_done) {
            $css_done = true;
    
            $css = <<<CSS
    /* ========= SHS Mega Menu: Categories hover + spacing fix ========= */
    
    /* Scope to XStore mega menu area (broad but safe) */
    .etheme-elementor-nav-menu,
    .etheme-elementor-mega-menu,
    .header-wrapper,
    .et_b-header,
    .et-header-wrapper {}
    
    /* 1) Stop any theme/elementor rule from changing hover color inside categories-mask */
    .etheme-elementor-nav-menu .categories-mask a,
    .etheme-elementor-mega-menu .categories-mask a,
    .header-wrapper .categories-mask a,
    .et_b-header .categories-mask a {
      color: inherit !important;
      text-decoration: none !important;
    }
    
    /* IMPORTANT: underline-only on hover/focus, gray underline, NO color change */
    .etheme-elementor-nav-menu .categories-mask a:hover,
    .etheme-elementor-nav-menu .categories-mask a:focus,
    .etheme-elementor-mega-menu .categories-mask a:hover,
    .etheme-elementor-mega-menu .categories-mask a:focus,
    .header-wrapper .categories-mask a:hover,
    .header-wrapper .categories-mask a:focus,
    .et_b-header .categories-mask a:hover,
    .et_b-header .categories-mask a:focus {
      color: inherit !important;                 /* <-- prevents primary color hover */
      text-decoration-line: underline !important;
      text-decoration-color: #8a8a8a !important; /* gray underline */
      text-decoration-thickness: 1px !important;
      text-underline-offset: 6px !important;
    }
    
    /* 2) Make underline length match text (not full row)
       If theme sets a to flex/block, force inline-block */
    .etheme-elementor-nav-menu .categories-mask a,
    .etheme-elementor-mega-menu .categories-mask a,
    .header-wrapper .categories-mask a,
    .et_b-header .categories-mask a {
      display: inline-block !important;
      width: auto !important;
    }
    
    /* 3) Fix uneven spacing between columns (h4 default margin + line-height differences) */
    .etheme-elementor-nav-menu .categories-mask h4,
    .etheme-elementor-mega-menu .categories-mask h4,
    .header-wrapper .categories-mask h4,
    .et_b-header .categories-mask h4 {
      margin: 0 !important;
      padding: 0 !important;
      line-height: 1.45 !important;
      font-weight: 400 !important;
      font-size: 15px !important;
      text-transform: none !important;
    }
    
    /* 4) Give each item consistent vertical spacing (same in both columns) */
    .etheme-elementor-nav-menu .categories-mask,
    .etheme-elementor-mega-menu .categories-mask,
    .header-wrapper .categories-mask,
    .et_b-header .categories-mask {
      margin: 0 0 12px 0 !important;
      padding: 0 !important;
    }
    CSS;
    
            $content .= '<style id="shs-mega-menu-hover-fix">' . $css . '</style>';
        }
    
        return $content;
    
    }, 20);
    
    

    It matches styles, but H4 is not switching.
    Can you please check and help me? This h4 tag broke the semantic structure of the website.
    Thanks

    Files is visible for topic creator and
    support staff only.
    3 Answers
    Avatar: Samir Malik
    Samir Malik
    Support staff
    January 28, 2026 at 08:24

    Hello,

    Please update the custom code to this:

    
    add_filter('elementor/frontend/the_content', function ($content) {
        
        if (stripos($content, 'categories-mask') === false) {
            return $content;
        }
    
        // Inject CSS only once
        static $css_done = false;
        if (!$css_done) {
            $css_done = true;
            $css = <<<CSS
    /* ... your CSS here ... */
    CSS;
            $content .= '<style id="shs-mega-menu-hover-fix">' . $css . '</style>';
        }
    
        // A) Add shs-mega-link class to anchors
        $content = preg_replace_callback(
            '#<div([^>]*class="[^"]*categories-mask[^"]*"[^>]*)>\s*<a([^>]*)>#i',
            function ($m) {
                $divOpen = '<div' . $m[1] . '>';
                $aAttrs  = trim($m[2]);
    
                if (preg_match('/\bclass=["\']([^"\']*)["\']/', $aAttrs, $cm)) {
                    if (stripos($cm[1], 'shs-mega-link') === false) {
                        $newClass = trim($cm[1] . ' shs-mega-link');
                        $aAttrs = preg_replace(
                            '/\bclass=["\']([^"\']*)["\']/i',
                            'class="' . esc_attr($newClass) . '"',
                            $aAttrs,
                            1
                        );
                    }
                } else {
                    $aAttrs .= ' class="shs-mega-link"';
                }
    
                return $divOpen . '<a' . $aAttrs . '>';
            },
            $content
        );
    
        // B) Add class to h4 tags (improved regex)
        $content = preg_replace_callback(
            '#<h4\b([^>]*)>#i',
            function ($m) use ($content) {
                // Only process h4 tags within categories-mask context
                $attrs = $m[1];
                
                if (preg_match('/\bclass=["\']([^"\']*)["\']/', $attrs, $cm)) {
                    if (stripos($cm[1], 'shs-mega-link-text') === false) {
                        $newClass = trim($cm[1] . ' shs-mega-link-text');
                        return '<h4' . preg_replace(
                            '/\bclass=["\']([^"\']*)["\']/i',
                            ' class="' . esc_attr($newClass) . '"',
                            $attrs,
                            1
                        ) . '>';
                    }
                } else {
                    return '<h4 class="shs-mega-link-text"' . $attrs . '>';
                }
                
                return $m[0];
            },
            $content
        );
    
        return $content;
    
    }, 20);
    

    Don’t forget to replace the /* ... your CSS here ... */ with your custom CSS codes.

    Best Regards,
    8Theme’s Team

    Avatar: Rok
    Rok
    Participant
    January 28, 2026 at 15:39

    Thanks for the support! My topic “How to change default meaning of semantic tag” has been successfully resolved.

    Avatar: Tony Rodriguez
    Tony Rodriguez
    Support staff
    January 28, 2026 at 15:39

    Dear Rok,

    It’s great having you in our WordPress & WooCommerce community!

    Every insight you share helps us refine XStore and build tools that empower thousands of online store owners worldwide.

    Together, we grow stronger with every release.

    Topic closed.
    The 8Theme Team

  • Viewing 4 results - 1 through 4 (of 4 total)

The issue related to '‘How to change default meaning of semantic tag’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.