Instantly share code, notes, and snippets.
Last active
January 19, 2016 17:27
-
Save dingo-d/73488da35e6a77e6cd1c to your computer and use it in GitHub Desktop.
A menu walker for adding sidebars in menu. The megamenu needs to be handled by javascript (or in any other way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Allow HTML descriptions in WordPress Menu | |
remove_filter( 'nav_menu_description', 'strip_tags' ); | |
function my_plugin_wp_setup_nav_menu_item( $menu_item ) { | |
if ( isset( $menu_item->post_type ) && 'nav_menu_item' == $menu_item->post_type) { | |
$menu_item->description = apply_filters( 'nav_menu_description', $menu_item->post_content ); | |
} | |
return $menu_item; | |
} | |
add_filter( 'wp_setup_nav_menu_item', 'my_plugin_wp_setup_nav_menu_item' ); | |
// Menu | |
class yourcustom_walker_nav_menu extends Walker_Nav_Menu { | |
public function display_element($el, &$children, $max_depth, $depth = 0, $args, &$output){ | |
$id = $this->db_fields['id']; | |
if(isset($children[$el->$id])){ | |
$el->classes[] = 'has_children'; | |
} | |
parent::display_element($el, $children, $max_depth, $depth, $args, $output); | |
} | |
// add classes to ul sub-menus | |
function start_lvl( &$output, $depth = 0, $args = array() ) { | |
// depth dependent classes | |
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent | |
$display_depth = ( $depth + 1); // because it counts the first submenu as 0 | |
$classes = array( | |
'navi', | |
( $display_depth == 1 ? 'first' : '' ), | |
( $display_depth >= 2 ? 'navi' : '' ), | |
'menu-depth-' . $display_depth | |
); | |
$class_names = implode( ' ', $classes ); | |
// build html | |
$output .= "\n" . $indent . '<ul class="' . esc_attr($class_names) . '">' . "\n"; | |
} | |
// add main/sub classes to li's and links | |
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { | |
global $wp_query; | |
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent | |
static $is_first; | |
$is_first++; | |
// depth dependent classes | |
$depth_classes = array( | |
( $depth == 0 ? 'main-menu-item' : '' ), | |
( $depth >= 2 ? 'navi' : '' ), | |
( $is_first == 1 ? 'menu-first' : '' ), | |
'menu-item-depth-' . $depth | |
); | |
$depth_class_names = esc_attr( implode( ' ', $depth_classes ) ); | |
// passed classes | |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; | |
$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) ); | |
$is_mega_menu = (strpos($class_names,'mega') !== false) ? true : false; | |
$use_desc = (strpos($class_names,'use_desc') !== false) ? true : false; | |
$is_sidebar = (strpos($class_names,'menu_sidebar') !== false) ? true : false; | |
$no_title = (strpos($class_names,'no_title') !== false) ? true : false; | |
// build html | |
$output .= $indent . '<li id="nav-menu-item-'. esc_attr($item->ID) . '" class="' . esc_attr($depth_class_names) . ' ' . esc_attr($class_names) . '">'; | |
// link attributes | |
$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="' . (($item->url[0] == "#" && !is_front_page()) ? home_url() : '') . esc_attr($item->url) .'"' : ''; | |
$attributes .= ' class="menu-link '.((strpos($item->url,'#') === false) ? '' : 'scroll').' ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"'; | |
$html_output = ($use_desc) ? '<div class="description_menu_item">'.wp_kses($item->description, allowed_tags()).'</div>' : ''; | |
if ($is_sidebar) { | |
ob_start(); | |
dynamic_sidebar($item->description); | |
$sidebar_html = ob_get_clean(); | |
$sidebar_output = '<div class="sidebar_menu_item">'.$sidebar_html.'</div>'; | |
$item_output = $sidebar_output; | |
} else{ | |
$item_output = (!$no_title) ? '<a ' . $attributes . '><span>' . apply_filters( 'the_title', $item->title, $item->ID ) . '</span></a>'. $html_output : $html_output; | |
} | |
// build html | |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ).(($is_mega_menu)?'<div class="sf-mega"><div class="sf-mega-inner clearfix">':''); | |
} | |
function end_el( &$output, $item, $depth = 0, $args = array() ) { | |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; | |
$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) ); | |
$is_mega_menu = (strpos($class_names,'mega') !== false) ? true : false; | |
$output .= (($is_mega_menu)?'</div></div>':'') . "</li>\n"; | |
} | |
} | |
//Sidebars in Menu | |
if ( !class_exists('sidebars_custom_menu')) { | |
class sidebars_custom_menu { | |
public function add_nav_menu_meta_boxes() { | |
add_meta_box( | |
'sidebar_menu_add', | |
esc_html__('Add Sidebar', 'yourtheme'), | |
array( $this, 'nav_menu_link'), | |
'nav-menus', | |
'side', | |
'low' | |
); | |
} | |
public function nav_menu_link() {?> | |
<div id="posttype-sidebars" class="posttypediv"> | |
<div id="tabs-panel-sidebars" class="tabs-panel tabs-panel-active"> | |
<ul id ="sidebars-checklist" class="categorychecklist form-no-clear"> | |
<?php | |
$i = -1; | |
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) { | |
ob_start(); | |
dynamic_sidebar($sidebar['id']); | |
$sidebar_html = ob_get_clean(); | |
?> | |
<li> | |
<label class="menu-item-title"> | |
<input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-object-id]" value="<?php echo $sidebar['id']; ?>"> <?php echo ucwords( $sidebar['name'] ); ?> | |
</label> | |
<input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-type]" value="custom"> | |
<input type="hidden" class="menu-item-attr-title" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-attr-title]" value="<?php echo ucwords( $sidebar['name'] ); ?>"> | |
<input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value=" " /> | |
<input type="hidden" class="menu-item-url" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-url]" value=""> | |
<input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-classes]" value="menu_sidebar"> | |
<input type="hidden" class="menu-item-description" name="menu-item[<?php echo esc_attr($i); ?>][menu-item-description]" value="<?php echo $sidebar['id']; ?>"> | |
</li> | |
<?php | |
$i --; | |
} | |
?> | |
</ul> | |
</div> | |
<p class="button-controls"> | |
<span class="list-controls"> | |
<a href="<?php echo admin_url( 'nav-menus.php?page-tab=all&selectall=1#posttype-sidebars' ); ?>" class="select-all"><?php esc_html_e('Select All', 'yourtheme'); ?></a> | |
</span> | |
<span class="add-to-menu"> | |
<input type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_html_e('Add to Menu', 'yourtheme');?>" name="add-post-type-menu-item" id="submit-posttype-sidebars"> | |
<span class="spinner"></span> | |
</span> | |
</p> | |
</div> | |
<?php } | |
} | |
} | |
$custom_nav = new sidebars_custom_menu; | |
add_action('admin_init', array($custom_nav, 'add_nav_menu_meta_boxes')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment