WordPress nav_menu_link_attributes Not Working

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.

Leave a Comment