Want more features on Pastebin? Sign Up, it's FREE!

Allow HTML in WordPress Custom Menu Descriptions

By: gregrickaby on May 9th, 2012  |  syntax: PHP  |  size: 2.59 KB  |  views: 112  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Create HTML list of nav menu items and allow HTML tags.
  3.  * Replacement for the native menu Walker, echoing the description.
  4.  * This is the ONLY known way to display the Description field.
  5.  *
  6.  * @see http://wordpress.stackexchange.com/questions/51609/
  7.  *
  8. */
  9. class Description_Walker extends Walker_Nav_Menu {
  10.  
  11.     function start_el(&$output, $item, $depth, $args)
  12.     {
  13.         $classes     = empty ( $item->classes ) ? array () : (array) $item->classes;
  14.  
  15.         $class_names = join(
  16.             ' '
  17.         ,   apply_filters(
  18.                 'nav_menu_css_class'
  19.             ,   array_filter( $classes ), $item
  20.             )
  21.         );
  22.  
  23.         ! empty ( $class_names )
  24.             and $class_names = ' class="'. esc_attr( $class_names ) . '"';
  25.  
  26.                 // Build default menu items
  27.         $output .= "<li id='menu-item-$item->ID' $class_names>";
  28.  
  29.         $attributes  = '';
  30.  
  31.         ! empty( $item->attr_title )
  32.             and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
  33.         ! empty( $item->target )
  34.             and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
  35.         ! empty( $item->xfn )
  36.             and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
  37.         ! empty( $item->url )
  38.             and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';
  39.  
  40.                 // Build the description (you may need to change the depth to 0, 1, or 2)
  41.         $description = ( ! empty ( $item->description ) and 1 == $depth )
  42.             ? '<span class="nav_desc">'.  $item->description . '</span>' : '';
  43.  
  44.         $title = apply_filters( 'the_title', $item->title, $item->ID );
  45.  
  46.         $item_output = $args->before
  47.             . "<a $attributes>"
  48.             . $args->link_before
  49.             . $title
  50.             . '</a> '
  51.             . $args->link_after
  52.             . $description
  53.             . $args->after;
  54.  
  55.         // Since $output is called by reference we don't need to return anything.
  56.         $output .= apply_filters(
  57.             'walker_nav_menu_start_el'
  58.         ,   $item_output
  59.         ,   $item
  60.         ,   $depth
  61.         ,   $args
  62.         );
  63.     }
  64. }
  65.  
  66. // Allow HTML descriptions in WordPress Menu
  67. remove_filter( 'nav_menu_description', 'strip_tags' );
  68. add_filter( 'wp_setup_nav_menu_item', 'cus_wp_setup_nav_menu_item' );
  69. function cus_wp_setup_nav_menu_item( $menu_item ) {
  70.      $menu_item->description = apply_filters( 'nav_menu_description', $menu_item->post_content );
  71.      return $menu_item;
  72. }
  73.  
  74. and then use this in your template:
  75.  
  76. <?php wp_nav_menu( array( 'walker' => new Description_Walker )); ?>
clone this paste RAW Paste Data