Skip to content

Instantly share code, notes, and snippets.

@m-e-h
Created May 7, 2016 18:34
Show Gist options
  • Save m-e-h/c1bd164856546f3313fc48205188622c to your computer and use it in GitHub Desktop.
Save m-e-h/c1bd164856546f3313fc48205188622c to your computer and use it in GitHub Desktop.
Custom class selectors for WP nav items
<?php
/**
* Class selectors added to the menu item's <li> element.
*/
function menu_item( $attr, $item ) {
return $attr;
}
add_filter( 'nav_menu_css_class', 'menu_item', 10, 2 );
/**
* Class selectors added to the menu item's <a> element.
*/
function menu_link( $attr, $item, $args ) {
return $attr;
}
add_filter( 'nav_menu_link_attributes', 'menu_link', 10, 3 );
/**
* Class selectors added for the menu item's state.
*/
function nav_menu_filters( $text ) {
$replace = array(
'current_page_item' => 'is-active',
'current_page_parent' => 'is-active',
'current_page_ancestor' => 'is-active',
'current-menu-item' => 'is-active',
'menu-item-has-children' => 'has-dropdown',
'sub-menu' => 'dropdown',
);
$text = str_replace( array_keys( $replace ), $replace, $text );
return $text;
}
add_filter( 'wp_nav_menu', 'nav_menu_filters' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment