So I’m adding descriptions to my site’s main menus. Checked the box, added the text. Dropped in a custom walker in functions.php. Dropped in a wp_nav_menu call in header.php to call the custom walker. Added text to the descriptions.
All good… but then I decided I wanted to add breaks via HTML… Dammit they’re being stripped out. Oh, right, I need to turn off that filter. So I do, via the correct line in functions.php. ONly, it doesn’t work. The HTML is still being stripped.
Before I go on, here’s the walker I’m using with the filter removal line after it:
#new walker to enable description text under menu items.
class description_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$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="' . esc_attr( $item->url ) .'"' : '';
$description = ! empty( $item->description ) ? '<p class="menu_description">'.esc_attr( $item->description ).'</p>' : '';
if($depth != 0)
{
$description = $append = $prepend = "";
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
## Function to stop HTML removal from menu descriptions
remove_filter('nav_menu_description', 'strip_tags');
More info: The HTML is recorded in the database, so it’s present there… just not on output. This is in WordPress 3.3. For clarity, here’s the code that calls the menu in header.php:
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'depth' => 0,
'walker' => new description_walker())
);
?>
I have that sinking feeling I’m missing something basic and just need a new pair of eyes on this… any ideas?
in a new wordpress version, the remove_filter isn’t enough, cause the desc is stripped hardcoded from post_content …
this will do the trick:
The problem is not the DB doesn’t contain the HTML, the filter seems to work at getting the information saved, the issue is that the menu administration page strips HTML, it’s written into the walker that displays that page.
Take a look at line 167 here.
http://core.trac.wordpress.org/browser/tags/3.3/wp-admin/includes/nav-menu.php#L164
HTML is escaped when it’s displayed, because naturally it’s not suppose to support HTML, i’m not sure where that occurs exactly when displaying the nav menu frontside, but doing a little debugging and dumping of the
$item
variable lead to a discovery.$item->post_content
will hold the description, but with the HTML!!… use that in place of$item->description
and you should get the result you’re after.. 😉Hope that helps.. 🙂