Adding line breaks to nav menu items

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 );

Leave a Comment