Skip to content

Instantly share code, notes, and snippets.

@ontiuk
Created February 20, 2018 00:44
Show Gist options
  • Save ontiuk/ae8ca2a629208439b69be2be7c625cb7 to your computer and use it in GitHub Desktop.
Save ontiuk/ae8ca2a629208439b69be2be7c625cb7 to your computer and use it in GitHub Desktop.
WordPress - Nav - Remove injected list item attributes
/**
* Themify menu links
*
* - Trim injected classes
*/
function theme_nav_link( $classes, $item ) {
// Tidy up li classes
if ( ( $key = array_search( 'menu-item-type-post_type', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'menu-item-type-custom', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'menu-item-object-page', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'menu-item-object-custom', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'menu-item-home', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'current_page_item', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'current-menu-item', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'page_item', $classes ) ) !== false ) { unset($classes[$key]); }
if ( ( $key = array_search( 'page-item-46', $classes ) ) !== false ) { unset($classes[$key]); }
foreach ( $classes as $k=>$v ) {
if ( preg_match( '/page-item-(\d+)$/', $v ) ) { unset($classes[$k]); }
if ( preg_match( '/menu-item-(\d+)$/', $v ) ) { unset($classes[$k]); }
if ( preg_match( '/nav-item-(\d+)$/', $v ) ) { unset($classes[$k]); }
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'theme_nav_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment