8

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

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().

2
  • This must be specific to multisites since an admin on a single-site install can use the <br/> tag... In any case menu items are post types, so I think the sanitisation is in sanitize_post_field() (specifically title_save_pre - see source). Unfortunately there's no way to circumvent that for just menu titles - so your posted solution is probably the best. Commented Jul 10, 2013 at 8:49
  • Yeah I thought I remembered that you could... so never realized it was a problem until a client complained. Thanks for the source link. Commented Jul 10, 2013 at 13:41

2 Answers 2

12

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 );
5
  • 1
    Given that it's not really regular expression, I'd just use generic str_ireplace() for replace. Also didn't know is_nav_menu_item(), cool. :)
    – Rarst
    Commented Jul 10, 2013 at 0:08
  • 1
    I just discovered is_nav_menu_item() while reading through some code for this question. It was new to me too. Thanks for your help! Commented Jul 10, 2013 at 0:38
  • There is a way to no affect all the titles? Because this affect all, even in dashboard. Commented Jun 4, 2015 at 22:39
  • Just check that it isn't is_admin() then. Commented Jun 5, 2015 at 3:58
  • Nice, and with "<br class='break' />" you can make it responsive. Commented Sep 4, 2016 at 21:34
3

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.

2
  • Yup! That's exactly what I just discovered. I ended up using #BR# as a safe character. Will post. Commented Jul 10, 2013 at 0:03
  • How you can do it to no affect all the titles? Commented Jun 4, 2015 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.