DAC of Design Action
9/6/2016 - 5:01 PM

Submenu Shortcode with Menu ID

Submenu Shortcode with Menu ID

##Purpose Allows admins to manually choose which submenu to display when using the [submenu] shortcode. Useful when combined with widget logic to display a sidebar menu on pages/archives/templates not in the main navigation.It's also helpful when you need to show a non-default sidebar menu for a specific template.

##Installation The code is meant to completely replace the original submenu plugin code (dac-sidebar-menus.php).It doesn't change the default behavior, it just allows the submenu shortcode to accept a menu_id parameter.

##Use Enter the ID of the submenu you want to display: [submenu menu_id='2789'] To find the menu ID, inspect the main menu. The menu ID is the four digit number following "menu-item-": <li id="menu-item-2345">About</li>

<?php
/*
Plugin Name: Design Action - Sidebar Menus
Description: Creates submenus (for use in sidebars) based on the primary menu, including active states, etc. Use with the shortcode [submenu] (within a widget) or theme function display_the_submenu();
Version:     0.0.1
Author:      Design Action
Author URI:  http://designaction.org
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );

// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
  if ( isset( $args->sub_menu ) ) {
    $root_id = 0;
   
    // find the current menu item
    foreach ( $sorted_menu_items as $menu_item ) {
      if ( $menu_item->current ) {
        // set the root id based on whether the current menu item has a parent or not
        $root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
        break;
      }
    }

    //override menu id if 'menu_id' parameter is specified in shortcode
    if ($args->manual_menu_id != '') {
      $root_id = $args->manual_menu_id;
    }
    // find the top level parent
    if ( ! isset( $args->direct_parent ) ) {
      $prev_root_id = $root_id;
      while ( $prev_root_id != 0 ) {
        foreach ( $sorted_menu_items as $menu_item ) {
          if ( $menu_item->ID == $prev_root_id ) {
            $prev_root_id = $menu_item->menu_item_parent;
            // don't set the root_id to 0 if we've reached the top of the menu
            if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
            break;
          } 
        }
      }
    }

    $menu_item_parents = array();
    foreach ( $sorted_menu_items as $key => $item ) {
      // init menu_item_parents
      if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;

      if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
        // part of sub-tree: keep!
        $menu_item_parents[] = $item->ID;
      } else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
        // not part of sub-tree: away with it!
        unset( $sorted_menu_items[$key] );
      }
    }
    
    return $sorted_menu_items;
  } else {
    return $sorted_menu_items;
  }
}

function display_the_submenu($atts) {
  //set menu_id default
  $a = shortcode_atts( array(
          'menu_id' => ''
      ), $atts );

    return wp_nav_menu( array(
      'theme_location' => 'primary-menu',
      'sub_menu' => true,
      'show_parent' => true,
      'direct_parent' => false,
      'echo' => false,
      'manual_menu_id' => $a['menu_id']
    ) );
}
// Register Shortcodes 
function register_shortcodes(){
   add_shortcode('submenu', 'display_the_submenu');
}
add_action( 'init', 'register_shortcodes');