Skip to content

Instantly share code, notes, and snippets.

@imgiseverything
Last active October 27, 2015 15:20
Show Gist options
  • Save imgiseverything/30843bcab992246b172c to your computer and use it in GitHub Desktop.
Save imgiseverything/30843bcab992246b172c to your computer and use it in GitHub Desktop.
/**
* Place this code in your theme's functions.php file to make the HTML
* it produces is more BEM friendly.
* This works out the menu_class value from wp_nav_menu() and uses that to create
* the class values.
*/
// Add a class to wp_nav_menu <li> tags so you get <li class="{menu_class}_item">
add_filter('nav_menu_css_class' , 'my_nav_special_class' , 10 , 3);
function my_nav_special_class($classes, $item, $args){
$classes[] = $args->menu_class . '__item';
return $classes;
}
// Add a class to wp_nav_menu's <a> tags so you get <a class="{menu_class}__link">
add_filter('nav_menu_link_attributes' , 'my_nav_special_attribute' , 10 , 3);
function my_nav_special_attribute($attributes, $item, $args){
$attributes['class'] = $args->menu_class . '__link';
return $attributes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment