I am attempting to add a data attribute to all menu items, but it's simply not working. I am using wp_nav_menu to call my menu walker as well.

function menu_anchor_attributes ( $atts, $item, $args ) {
    $atts['data-menuanchor'] = $item->attr_title;
return $atts;
add_filter( 'nav_menu_link_attributes', 'menu_anchor_attributes', 10, 3 );

I am using JointsWP as my framework, which includes this walker:

// The Top Menu
function joints_top_nav() {
        'container' => false,                           // Remove nav container
        'menu_class' => 'horizontal menu',       // Adding custom nav class
        'items_wrap' => '<ul id="%1$s" class="%2$s" data-responsive-menu="accordion medium-dropdown">%3$s</ul>',
        'theme_location' => 'main-nav',                 // Where it's located in the theme
        'depth' => 5,                                   // Limit the depth of the nav
        'fallback_cb' => false,                         // Fallback function (see below)
        'walker' => new Topbar_Menu_Walker()

// Big thanks to Brett Mason (https://github.com/brettsmason) for the awesome walker
class Topbar_Menu_Walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = Array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"menu\">\n";
  • Could you edit your question ( using the edit link above ) and add in your Walker Class please? It could be that it doesn't call the filter.
    – Howdy_McGee
    Aug 24, 2016 at 18:26
  • Of course, I have included the walker. Aug 24, 2016 at 18:33
  • What happens if you put ( ! empty( $item->attr_title ) ) ? $item->attr_title : 'test'; in your attributes filter? I have a suspicion that attr_title is an empty string and the attribute is being ignored because of this.
    – Howdy_McGee
    Aug 24, 2016 at 18:38
  • Nothing happens, unfortunately. Not even an empty data-menuanchor attribute displays. Aug 24, 2016 at 19:35
  • Something else is the issue then, if I paste the provided code above into my theme and add the conditional in the comment above - it adds the attribute to the anchor tag as expected. Try removing this functionality from your theme and add it to a WordPress default theme like Twenty Fifteen or something of the sort. If you're able to get it working - from there you'll need to disable all the installed plugins and tear your theme apart until you figure out what is causing the discrepancy.
    – Howdy_McGee
    Aug 24, 2016 at 19:39

2 Answers 2


Whenever an attribute is empty WordPress filters decide just not to show the attribute so a simple test for this is the following:

function menu_anchor_attributes ( $atts, $item, $args ) {
    $atts['data-menuanchor'] = ( ! empty( $item->attr_title ) ) ? $item->attr_title : 'test';
    return $atts;
add_filter( 'nav_menu_link_attributes', 'menu_anchor_attributes', 10, 3 );

This way, if a title attribute hasn't been filled into the back-end it will still display the attribute with a value of test.


The fiter nav_menu_link_attributes only works on menus that are created in wp-admin. By default wp_nav_menu returns the published pages even if you did not create a menu on the backend but as soon as you click "create menu" on Appearance -> Menus the filter does its job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.