Sunday, July 7, 2013

How to Remove Whitespace from Function Output in wordpress

There are a lot of functions in WordPress that add whitespace. For example, wp_list_pages() outputs a list of all your published pages. The HTML markup it spits out is pretty nicely formatted (meaning: has line breaks and indenting).

There are some circumstances where all that "whitespace" in the formatting is undesirable. Like 1) it's all the more characters to deliver and 2) closing "the gap" in older versions of IE.

If the php function supports a way to return a string (rather than immediately echo it), you can use a regex to remove the space:

echo preg_replace('/>\s+</m', '><', wp_list_pages('echo=0'));
Disqus Comments