title: Wordpress - Dynamically injecting menu items using Walker Class
body: <p>I am trying to pull in some dynamic (non-WP) content to populate a menu in Wordpress. For this I've extended the Walker Class as shown here:</p>
  
  <p><a href="http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output" rel="nofollow">http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output</a></p>
  
  <p>So my Menu would look like</p>
  
  <ul>
  <li>Home </li>
  <li>Blog     </li>
  <li>News</li>
  <li>Features</li>
  <li>competitions -
  <ul>
  <li>Comp1    </li>
  <li>Comp2</li>
  </ul></li>
  </ul>
  
  <p>Where Comp1 and Comp2 are pulled from a DB on a seperate site. They are just links to an external site, so the only relevant value is the 'Comp Title' and 'Comp URL'</p>
  
  <p>The main method within my class is : </p>
  
  <pre><code> function start_el(&amp;$output, $item, $depth, $args)
      {
  
          global $wp_query;
          $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  
          $class_names = $value = '';
  
          $classes = empty( $item-&gt;classes ) ? array() : (array) $item-&gt;classes;
  
          $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
          $class_names = ' class="'. esc_attr( $class_names ) . '"';
  
          $output .= $indent . '&lt;li id="menu-item-'. $item-&gt;ID . '"' . $value . $class_names .'&gt;';
  
          $attributes  = ! empty( $item-&gt;attr_title ) ? ' title="'  . esc_attr( $item-&gt;attr_title ) .'"' : '';
          $attributes .= ! empty( $item-&gt;target )     ? ' target="' . esc_attr( $item-&gt;target     ) .'"' : '';
          $attributes .= ! empty( $item-&gt;xfn )        ? ' rel="'    . esc_attr( $item-&gt;xfn        ) .'"' : '';
          $attributes .= ! empty( $item-&gt;url )        ? ' href="'   . esc_attr( $item-&gt;url        ) .'"' : '';
  
          $prepend = '&lt;strong&gt;';
          $append = '&lt;/strong&gt;';
          $description  = ! empty( $item-&gt;description ) ? '&lt;span&gt;'.esc_attr( $item-&gt;description ).'&lt;/span&gt;' : '';
  
          if($depth != 0)
          {
              $description = $append = $prepend = "";
          }
  
          if($item-&gt;title == 'Competitions')
          {
              $item_output = $args-&gt;before;
              $item_output .= '&lt;a'. $attributes .'&gt;';
              $item_output .= $args-&gt;link_before .$prepend.apply_filters(  'the_title', $item-&gt;title, $item-&gt;ID ).$append;
              $item_output .= $description.$args-&gt;link_after;
              $item_output .= '&lt;/a&gt;';
              $item_output .= $args-&gt;after;
  
              $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  
              $this-&gt;loopComps($output , $args);
  
          }
          else
          {
              $item_output = $args-&gt;before;
              $item_output .= '&lt;a'. $attributes .'&gt;';
              $item_output .= $args-&gt;link_before .$prepend.apply_filters( 'the_title', $item-&gt;title, $item-&gt;ID ).$append;
              $item_output .= $description.$args-&gt;link_after;
              $item_output .= '&lt;/a&gt;';
              $item_output .= $args-&gt;after;
  
              $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
          }
      }
  </code></pre>
  
  <p>and then the loopComps method is:
  (getGalleryComps() pulls in the dynamic content)</p>
  
  <pre><code> function loopComps($output , $args)
      {
          $openComps = $this-&gt;getGalleryComps();
          foreach($openComps as $comp)
          {
              $item =  new StdClass;
              $item-&gt;ID = 9999;
              $item-&gt;post_author = 5;
              $item-&gt;post_date = '2012-11-16 10:48:44';
              $item-&gt;post_date_gmt = '2012-11-16 10:48:44';
              $item-&gt;title = $comp['competition_name'];
              $item-&gt;post_type = 'nav_menu_item';
              $item-&gt;post_name = $comp['competition_name'];
              $item-&gt;post_title = $comp['competition_name'];
              $item-&gt;post_excerpt = $comp['competition_name'];
              $item-&gt;guid = $comp['competition_name'];
              $item-&gt;url = 'http://www.mycomps';
              $item-&gt;post_status = 'publish';
              $item-&gt;post_parent = 0;
              $item-&gt;filter = 'raw';
              $item-&gt;menu_item_parent = '6845';
              $item-&gt;object_id = '99999';
              $item-&gt;object = 'custom';
              $item-&gt;type = 'custom';
              $item-&gt;classes = array(null , 'menu-item' , 'menu-item-type-custom' , 'menu-item-object-custom');
              $item-&gt;menu_order = 6;
  
  
              return $this-&gt;start_el($output, $item, 1, $args);
  
  
          }
  
      }
  </code></pre>
  
  <p>This all seems to work fine , expect the $item is never attached to the actual menu. If I print out the $items during the nav menu loop, I can see my dynamic content which I've created a 'psuedo Post' is there , it just never gets attached to the menu.</p>
  
  <p>Is there an easier way of injecting menu items? </p>
  
  <p>UPDATE 1:</p>
  
  <p>I've adjusted loopComps method to be: </p>
  
  <pre><code> function loopComps($output)
      {
          $openComps = $this-&gt;getGalleryComps();
  
          $output .= '&lt;ul&gt;';
          foreach( $openComps as $openComp )
          {
              $output .= '&lt;li&gt;&lt;a href='.$openComp-&gt;url.'&gt;'.$openComp['competition_name]'].'&lt;/a&gt;&lt;/li&gt;';
          }
          $output .= '&lt;/ul&gt;';
  
          return $output;
    }
  </code></pre>
  
  <p>As this does make much more sense , but doesn't solve the issue, the data is there, but is never shown in the menu. </p>