Skip to content

current-menu-item does not get applied to custom links in navigation block #61266

Description

@ethanclevenger91

Description

I expect custom links within the navigation block to get the current-menu-item class when on that link on the site, as was the case for classic nav menus. Currently, they do not receive this class.

Step-by-step reproduction instructions

  1. New WP site (at, say, example.test)
  2. Install TT4
  3. Create new navigation menu via Editor
  4. Add "Custom Link" to navigation using the WordPress install's URL and some slug, such as "http://example.test/link"
  5. Create a new page titled "Link" (or otherwise) so that its permalink matches the menu item you created, such as "http://example.test/link"
  6. Inspect nav menu on website front end
  7. See no current-menu-item class

For quick reproduction, these steps are a bit absurd. The issue is more reasonably encountered when, for instance, you have a custom taxonomy. The prepared nav blocks (like Category Link or Tag Link or Page Link) do not include custom taxonomy term archive links, so you must use a Custom Link item, and the correct classes are not applied in that scenario, either.

Screenshots, screen recording, code snippet

No response

Environment info

WP 6.5.2, Gutenberg 18.2.0. Also present w/o Gutenberg

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Activity

  1. jhimross commented on May 10, 2024

    @jhimross

    I replicated the issue and can reproduce it by following the instructions above. I tested this using a fresh WP installation with WP version 6.5.2 and using Twenty Twenty Four theme. Watch this when adding menu using "custom link":- https://share.zight.com/E0ueEXKD

    And this what will happen, when I added a menu item not using the "custom link" feature, it shows this aria-current="page". See this: https://share.zight.com/JruE7pEn

  2. chuckpearson commented on May 29, 2024

    @chuckpearson

    I can replicate this issue and have the same problem. When I add a menu item to the navigation in Site Editor, if the item is a custom post type, the menu item does not get the current-menu-item class applied and does not get the aria-current either.

  3. jeroensmeets commented on May 30, 2024

    @jeroensmeets

    For anyone facing this issue, I have added this filter to functions.php as a fix. Replace str_ends_with if you're on PHP < 8.

    // add current-menu-item class to custom links in navigation block
    function filter_custom_link_block( $html ) {
        // this works for page links
        if ( false !== strpos( $html, 'current-menu-item' ) ) {
            return $html;
        }
    
        preg_match_all( '/href=["\']?([^"\'>]+)["\']?/', $html, $matches );
    
        if ( count( $matches ) > 1 ) {
            global $wp;
            $current_url = trailingslashit( home_url( $wp->request ) );
            $menuitem_url = trailingslashit( $matches[1][0] );
    
            if ( str_ends_with( $current_url, $menuitem_url ) ) {
                return str_replace( 'wp-block-navigation-link', 'wp-block-navigation-link current-menu-item', $html );
            }
        }
    
        return $html;
    }
    add_filter( 'render_block_core/navigation-link', 'filter_custom_link_block', 10 );
    
  4. github-actions commented on Jun 30, 2024

    @github-actions

    Hi,
    This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps.
    Thanks for helping out.

  5. added
    [Status] StaleGives the original author opportunity to update before closing. Can be reopened as needed.
    on Jun 30, 2024
  6. getdave commented on Nov 21, 2024

    @getdave
    Contributor

    I've reviewed this and in WP 6.7 when you add a link to the Navigation block using the Custom Link variation it will save as type: page if you select from the search results.

    <!-- wp:navigation-link {"label":"Custom Link Again","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->
    

    However, if you type the URL out (or copy/paste) manually then it will persist as a custom link and not be annotated as a page.

    <!-- wp:navigation-link {"label":"Manually Added","url":"http://localhost:8888/?page_id=2","kind":"custom"} /-->
    
    Video demonstrating different link types
    Screen.Capture.on.2024-11-21.at.09-36-29.mp4

    For the later scenario WordPress cannot "know" that this is an internal link without doing an operation similar to that shown in the comment above. As a result no "current" class will be applied.

    We could implement something like this but it would have to be run on every single custom link in the Navigation which might have performance implications.

    Given the way the Navigation block now defaults to inserting the "page" variation of the Navigation Link block, I feel it's increasingly unlikely that users will be linking to internal pages using the custom link variation.

    The only scenario I can envisage is the need to accommodate internal links such as http://localhost:8888/?page_id=2#someinternalanchor where someinternalanchor is a "deep link" anchor point on the page. We don't support that in the Link UI so that may require users to work around this using the custom link.

    Therefore I propose that we upgrade the Link UI to accommodate the ability to link to internal anchor points.

    If there are any other work flows that I'm missing I'd be grateful if folks could let me know below in the comments 🙏

    What do you think of the proposal above?

  7. ethanclevenger91 commented on Jan 10, 2025

    @ethanclevenger91
    Author

    As I mentioned when I opened this ticket, the pre-baked nav items in Gutenberg are not comprehensive of every possible kind of internal link that could be created. There is, for example, no block for a custom taxonomy term archive. Now perhaps it also resolves in some intelligent way (like your Page example) with more recent versions of the editor - I haven't checked. But I would offer that as another use case.

    WordPress cannot "know" that this is an internal link

    Classic theme navigation handled this without issue. Obviously, I'm not working on this feature, so there are nuances I don't understand, but it seems like that logic should have been maintained.

  8. Marc-pi commented on Jan 26, 2025

    @Marc-pi
    • yep, that's an annoying bug, manual link does not get the li current-menu-item class nor the aria-current="page" on a:href
      • "Given the way the Navigation block now defaults to inserting the "page" variation of the Navigation Link block, I feel it's increasingly unlikely that users will be linking to internal pages using the custom link variation." sometimes it's quicker to copy the page the url instead of searching for it (ex: WooCommerce product page with too many pages)
      • yep, the anchor link use case is also another use case
    • another anyoning thing : if you move your website from preprod to prod, you have to edit all links, the base url is not replaced (perhaps it's another issue, but Dave would like for sure to be aware of this).
  9. voyager131 commented on Feb 10, 2025

    @voyager131
    Contributor

    I can also confirm the same behavior described above with a site I'm currently working on. Also, when a sub-menu item is selected, it appears the parent no longer gets the "current-menu-parent" class?

  10. wdburgdorf commented on Mar 1, 2025

    @wdburgdorf

    WP6.7.2, 2025 Theme, still broken.

  11. wdburgdorf commented on Mar 1, 2025

    @wdburgdorf

    The above code did not work for me. I fixed it with ChatGPT o3-mini-high:

    // add current-menu-item class to custom links in navigation block
    function filter_custom_link_block( string $html ): string {
        // Return early if already marked as current.
        if ( strpos( $html, 'current-menu-item' ) !== false ) {
            return $html;
        }
    
        // Extract the first href value.
        if ( preg_match( '/href=["\']([^"\']+)["\']/', $html, $match ) ) {
            global $wp;
            // Get the current URL.
            $current_url = home_url( $wp->request );
            $menuitem_url = $match[1];
    
            // Parse the menu item URL.
            $menuitem_parsed = wp_parse_url( $menuitem_url );
            // If the URL is relative (no scheme), convert it to an absolute URL.
            if ( ! isset( $menuitem_parsed['scheme'] ) ) {
                $menuitem_url = home_url( $menuitem_url );
                $menuitem_parsed = wp_parse_url( $menuitem_url );
            }
    
            $current_parsed = wp_parse_url( $current_url );
    
            // Ensure both URLs have a scheme and host and that they match.
            if (
                isset( $current_parsed['scheme'], $current_parsed['host'], $menuitem_parsed['scheme'], $menuitem_parsed['host'] ) &&
                $current_parsed['scheme'] === $menuitem_parsed['scheme'] &&
                $current_parsed['host'] === $menuitem_parsed['host']
            ) {
                // Normalize paths: if not set, treat as root ("/").
                $current_path = isset( $current_parsed['path'] ) ? untrailingslashit( $current_parsed['path'] ) : '';
                $menuitem_path = isset( $menuitem_parsed['path'] ) ? untrailingslashit( $menuitem_parsed['path'] ) : '';
    
                $current_path = $current_path === '' ? '/' : $current_path;
                $menuitem_path = $menuitem_path === '' ? '/' : $menuitem_path;
    
                // If paths match, mark as current.
                if ( $current_path === $menuitem_path ) {
                    $html = str_replace(
                        'wp-block-navigation-link',
                        'wp-block-navigation-link current-menu-item',
                        $html
                    );
                }
            }
        }
    
        return $html;
    }
    add_filter( 'render_block_core/navigation-link', 'filter_custom_link_block', 10 );
    
  12. removed
    [Status] StaleGives the original author opportunity to update before closing. Can be reopened as needed.
    on Jul 21, 2025
  13. 14 remaining items

  14. added theissue type on Dec 18, 2025
  15. shazzad commented on Dec 18, 2025

    @shazzad
    Member

    Reproduction Report

    Description

    This report validates whether the issue can be reproduced.

    Environment

    • WordPress: 6.9
    • PHP: 7.4.33
    • Server: nginx/1.27.5
    • Database: mysqli (Server: 8.0.33 / Client: mysqlnd 7.4.33)
    • Browser: Firefox 146.0
    • OS: Windows 10/11
    • Theme: Twenty Twenty-Four 1.4
    • MU Plugins:
      • Debug Log Notice 1.0
      • Homerunner Service 0.1.0
      • Object Cache Pro (MU) 1.19.0
    • Plugins:
      • Better Search Replace 1.4.10
      • Code Snippets 3.9.3
      • Gravity Forms 2.9.24
      • Gravity Forms Stripe Add-On 6.0.1.0
      • HomeRunner 2.0.7
      • HomeRunner Plugin Data 0.1.3
      • HomeRunner Rentals United 0.0.3
      • Loco Translate 2.8.0
      • Redis Object Cache 2.7.0
      • Shazzad Wp Logs 2.0.7
      • Simple Custom Post Order 2.5.11
      • Test Reports 1.2.1
      • WordPress Beta Tester 4.0.0
      • WP Crontrol 1.19.3
      • WP Mail SMTP 4.7.1

    Actual Results

    1. ✅ Error condition occurs (reproduced).

    Additional Notes

    Steps to Reproduce:

    • Activate TT4 theme.
    • Create a new post Test post.
    • Added the new post permalink to a Navigation block
    • Visiting the single post does not add the current-menu-item class to the menu item.

    Supplemental Artifacts

    "Test post" single template does not add the css class.

    Image

    "Block Properties" page link correctly adds the css class to the dom.

    Image
  16. added
    Needs DevReady for, and needs developer efforts
    and removed
    Needs TestingNeeds further testing to be confirmed.
    on Dec 18, 2025
  17. getdave commented on Dec 19, 2025

    @getdave
    Contributor

    Thanks for all the feedback and reproduction folks. It's very much appreciated.

    It seems to me that there must be a problem. Directly pasted URLs can be considered separately, but we should certainly solve for the case when someone links to an entity (Post, Page).

  18. getdave commented on Dec 19, 2025

    @getdave
    Contributor

    I took a quick look at this in regards to entity links (not static URLs):

    Root Cause

    In packages/block-library/src/navigation-link/index.php (line 214), the code checks if the queried object has a property matching the link's kind:

    $is_active = ! empty( $attributes['id'] )
        && get_queried_object_id() === (int) $attributes['id']
        && ! empty( get_queried_object()->$kind ); // ← This might fail for custom post types

    This third check likely fails for custom post types because the queried object structure might not match what the code expects. The kind check appears to have been a safeguard against a historical bug where id could be a string URL, but it might now be too restrictive.

    Solution

    Remove the kind property check and validate that id is numeric:

    1. Remove the ! empty( get_queried_object()->$kind ) check
    2. Ensure id is numeric and positive (prevents the string-URL bug)
    3. Keep the ID matching check

    Updated logic:

    $is_active = false;
    if ( ! empty( $attributes['id'] )
        && is_numeric( $attributes['id'] )
        && (int) $attributes['id'] > 0
    ) {
        $link_id = (int) $attributes['id'];
        if ( get_queried_object_id() === $link_id ) {
            $is_active = true;
        }
    }

    This should fix custom post types while maintaining safety against the historical bug. The ID match should be sufficient to determine if we're on the linked entity's page.

    We should start with a failing e2e test and work forwards from there.

  19. jhmonroe commented on Jan 30, 2026

    @jhmonroe

    @getdave thanks for your movement on this! who can assign it? def a big thing to fix to aid with adoption from classic themes to FSE

  20. getdave commented on Feb 6, 2026

    @getdave
    Contributor

    We also need to consider the new entity binding that's been added to the nav link block.

  21. getdave commented on Feb 6, 2026

    @getdave
    Contributor

    It's tracked in #38275.

    It's open for anyone to pick up and it even has a clear technical insight for people to follow.

    It might be possible to look at pre 7.0 but it's not guaranteed.

  22. getdave commented on Mar 13, 2026

    @getdave
    Contributor

    @jeryj would you agree that given that

    • custom links historically may have ids associated
    • custom links won't have block bindings associated

    ...we should be ok to remove the kind check and simply check for the presence of an ID that is number-like?

    Update: actually I realised it is not safe because I think Posts and Terms can have the same ID because they are in different DB tables!

  23. iamtakashi commented on Sep 3, 2026

    @iamtakashi

    Possibly useful context: I filed #82380 about how the insertion flow steers people into creating these URL-only links in the first place — the + appender defaults to Page Link, its search only looks at pages, and the Category/Tag Link variations are hidden behind "Browse all", so pasting a URL into the search field ends up being the natural fallback. It doesn't answer the rendering question discussed here, but if the picker created entity links by default, most of the menus hitting this wouldn't exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs DevReady for, and needs developer efforts[Block] NavigationAffects the Navigation Block[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions