Skip to content

Instantly share code, notes, and snippets.

@chriswinch
Last active March 21, 2019 21:40
Show Gist options
  • Save chriswinch/28043db607c2ff02be43fdd1a57652ca to your computer and use it in GitHub Desktop.
Save chriswinch/28043db607c2ff02be43fdd1a57652ca to your computer and use it in GitHub Desktop.
Custom Wordpress Nav Walker - Add your own classnames to WP_Nav_Menu
// Updated the Walker_Nav_Menu class to accept an array of classnames via argument
// Comments below show what has been changed / updated
// Original Walker_Nav_Menu copied from here: https://core.trac.wordpress.org/browser/tags/4.5.3/src//wp-includes/nav-menu-template.php#L0
<?php
class Nav_Walker extends Walker_Nav_Menu {
// Setup empty $data array
// Can be used to store any other data
private $data = [];
// Add __construct function to pull in array via $class argument
function __construct($class) {
// Assign $class array to $data['classes']
$this->data['classes'] = $class;
}
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
// Implode $data['classes'] array and assign to $custom_classes
// " / " is added to the end to separate your classes from built in WP classes
$custom_classes = implode(" ", $this->data['classes']) . " / ";
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
//Added esc_attr($custom_classes) to output your classes
$class_names = $class_names ? ' class="' . esc_attr($custom_classes) . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names .'>';
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
}
?>
// This allows you to add BEM style classnames ( or any other css methodology ) to your nav items
// Args:
// menu - Name of your wordpress menu as setup in WP Admin > Appearance > Menus
// menu_class - set the class of the <ul></ul> wrapper
// walker - An array of classes should be added as an argument
<?php wp_nav_menu(array('menu' => 'Wp Menu Name', 'container' => '', 'menu_class' => 'class-name', 'walker' => new Nav_Walker(['class-one', 'class-two', 'class-three']) )); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment