Tabindex attribute being filtered out of custom nav walker?

I have a custom nav walker that performs a number of functions. Because my navigation menus are hidden at the outset, and ought not be keyboard accessible until the menu button is clicked, I am trying to apply a tabindex attribute to the links in my navigation menu, but that attribute does not make it into the rendered code. Somewhere along the line, it is being filtered out. How do I stop this from happening? Here is my custom nav walker:

class custom_nav_walker extends Walker_Nav_Menu {

function start_lvl( &$output, $depth = 0, $args = array() ) {

$output .= ( $args->walker->has_children ) ? “<ul class=’sub-menu-container’><li class=’test-class’><ul>” : “”;
}

function end_lvl( &$output, $depth = 0, $args = array() ) {
$output .= ( $depth == 0 ) ? “</ul></li></ul>” : “”;
}

// Set break point variable for use later
var $break_point = null;
var $_child_counter = 0;

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

global $wp_query;

if( !isset( $this->current_menu ) )
$this->current_menu = wp_get_nav_menu_object( $args->menu );

if ( $depth == 1) $this->_child_counter++; else $this->_child_counter = 0;

// Set the break point count value
if ( !isset( $this->break_point ) && $depth == 1 )
$this->break_point = ceil( $item->_parent_children_count / 4 ) + 1;

$indent = ( $depth ) ? str_repeat( “t”, $depth ) : “”;

// Put the classes into an array
$classes = empty( $item->classes ) ? array() : (array) $item->classes;

// Add link title to class, for easy styling later
$item_title = strtolower( str_replace( ‘ ‘, ‘-‘, $item->title) );
$classes[] = ‘menu-name-‘ . $item_title;

$class_names = join( ‘ ‘, apply_filters( ‘nav_menu_css_class’, array_filter( $classes ), $item, $args ) );
$class_names = ‘ class=”‘ . esc_attr( $class_names ) . ‘”‘;

$id = apply_filters( ‘nav_menu_item_id’, ‘menu-item-‘. $item->ID, $item, $args );
$id = strlen( $id ) ? ‘ id=”‘ . esc_attr( $id ) . ‘”‘ : ”;

if ( $this->break_point == $this->_child_counter && $depth == 1 ) {
$output .= ‘</li></ul><ul><li’ . $id . $class_names .’>’;
$this->_child_counter = 0;
} else {
$output .= ‘<li’ . $id . $class_names .’>’;
}

$attributes = ! empty( $item->attr_title ) ? ‘ title=”‘ . esc_attr( $item->attr_title ) .'”‘ : ”;
$attributes .= ! empty( $item->target ) ? ‘ target=”‘ . esc_attr( $item->target ) .'”‘ : ”;
$attributes .= ! empty( $item->xfn ) ? ‘ rel=”‘ . esc_attr( $item->xfn ) .'”‘ : ”;
$attributes .= ! empty( $item->url ) ? ‘ href=”‘ . esc_attr( $item->url ) .'”‘ : ”;
$attributes .= ‘ tabindex=”-1″‘; // This is what is getting filtered out of the rendered HTML

$item_output = $args->before;
$item_output .= ‘<a’. $attributes .’ tabindex=”-1″>’;
$item_output .= $args->link_before . apply_filters( ‘the_title’, $item->title, $item->ID ) . $args->link_after;
$item_output .= ‘</a>’;
$item_output .= ($args->walker->has_children) ? ‘<i class=”dropdown-toggle fa fa-chevron-down” aria-hidden=”true”></i>’ : ”;
$item_output .= $args->after;

$output .= apply_filters( ‘walker_nav_menu_start_el’, $item_output, $item, $depth, $args, $id );
}

}

Read more here:: Tabindex attribute being filtered out of custom nav walker?

Leave a Reply

Your email address will not be published. Required fields are marked *