Adding line breaks to nav menu items

I need to add line breaks to the nav menu item titles. I didn’t realize this was a problem as when I am logged in as a Super Admin, I can add <br/> just fine, but apparently regular-level admins cannot.

I’ve read over this post
Custom Menus Description Stripping HTML Tags

Read More

but I’m fairly convinced the tags are being stripped on on save/update, so I am not immediately seeing how a Custom Walker is the solution, but my brain is pretty well shot today, so it might be obvious.

There also doesn’t seem to be any sanitation happening in wp_save_nav_menu_items() or wp_update_nav_menu_item().

Related posts

2 comments

  1. Following the hint from @Rarst regarding safe characters here’s what I ended up doing:

    function wpa_105883_menu_title_markup( $title, $id ){
        if ( is_nav_menu_item ( $id ) && ! is_admin() ){
            $title = preg_replace( '/#BR#/', '<br/>', $title );
        }
        return $title;
    }
    add_filter( 'the_title', 'wpa_105883_menu_title_markup', 10, 2 );
    

    Edit: Also per Rarst’s comment I’ve replaced the preg_replace with str_ireplace

    function wpa_105883_menu_title_markup( $title, $id ){
        if ( is_nav_menu_item ( $id ) ){
            $title = str_ireplace( "#BR#", "<br/>", $title );
        }
        return $title;
    }
    add_filter( 'the_title', 'wpa_105883_menu_title_markup', 10, 2 );
    
  2. Sans turning sanitization logic inside out or coding custom interface for it… I would just designate some safe character for it (for example pipe |) and replace it with break tag on output.

    It passes through the_title filter in walker, would only need to timely add and remove the filter so it does not affect titles elsewhere.

Comments are closed.