• Resolved Eric Mueller

    (@pnoeric)


    I’ve had to modify nav-menu-template.php to remove newlines and indents from the menu code it generates, to prevent a formatting problem. (The problem? Browsers actually pay attention to whitespace in <li> elements and add extra space. Try playing with this JSfiddle… see the extra space after ‘Option Two’? Now remove all the newlines in the menu HTML and it’ll go away. More explanation on this StackOverflow question and accepted answer.)

    Here’s my question: how can I make this solution (removing all newlines and tabs) something that is part of my theme’s functions.php file? For now I’ve just tweaked the WP core, which is fine… until I need to upgrade to the next version, which will put the whitespace back and break the formatting on my site. :-\ So I’m interested in keeping the core as it is, and having my code somehow hook into the output of nav-menu-template and strip all the whitespace.

    I’m guessing there’s an action that I can somehow tap into, and then I can just do a str_replace() to remove the whitespace. But I’m not sure of the specifics. Can someone help?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Removing newlines and tabs from nav-menu’ is closed to new replies.