• Hi there, I’m hoping someone could help out here.

    I’m looking for an easy, fool-proof way to get custom attributes into a WordPress menu link.

    You know how we can add “advanced menu properties” like “Link Target”, “Title Attribute”, “CSS Classes” and so on?

    I need a way to add custom attributes.

    This will help a lot with tracking in Google Analytics, if we want to set up certain links as events.

    For example, I’m hoping to be able to get this in the menu links (see where data-event-category and so on starts):

    <li id="menu-item-2773" class="nav li websites a menu-item menu-item-type-post_type menu-item-object-page menu-item-2773"><a href="https://www.mywebsite.com/menu-link" 
    
    data-event-category="top menu" 
    
    data-event-action="click" 
    
    data-event-label="click on first item" 
    
    itemprop="url"><span itemprop="name">Menu hypertext</span></a>

    I put spaces between them so they can be seen easier.

    (Note: those attributes are for use with the new Google Analytics autotrack.js plugin)

    I’ve been doing some research and the closest I could find is this article: https://www.wpexplorer.com/adding-custom-attributes-to-wordpress-menus/

    But the plugin this author made on the WP repository hasn’t been updated in 2 years, so I’m weary of that, or that it may be outdated.

    I see that ACF or another custom field method may work. But being a non-coder, I’m wondering if anyone out there has a ready-made solution, or even a dummy-proof step by step way to set this up?

    Your help would be so much appreciated!

  • The topic ‘How to get custom elements or attributes in WordPress menus?’ is closed to new replies.