WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Right now all the stuff that gets included in wp_head are left justified all the way in the code view.

Whereas all my other code that is around the wp_head is indented (tabbed) two times. Is it possible to add an indent/tabs to all the wp_head info?


share|improve this question
Why does this matter? – Wyck Aug 5 '13 at 23:23
It's perhaps a little OCD, but I think WP devs should have complete control over markup output. – jnthnclrk Aug 7 '13 at 9:58

Technically possibly, but probably not worth the effort (and overhead). If you inspect the source with something like Developer Tools in Chrome, your HTML will be automatically indented. In fact, some caching plugins (like W3 Total Cache) even remove all whitespace to improve page load times.

That said, if you want to ensure that your wp_head content is indented, you would need to do the following:

  1. Add a function to get_header which executes last. This function get all functions attached to wp_head (using $wp_filter['wp_head']), remove them, and reattach them to your own custom action (my_wp_head for example).
  2. A custom function (my_wp_head() for example) should then be attached to wp_head
  3. In the my_wp_head() function you would want to
    1. Create an array of match/replace regex patterns ($patterns = array("pattern"=>"replace pattern");). This pattern should trim whitespace and re-add tabs to the beginning of each line.
    2. Make a call to ob_start() to capture output
    3. Process the previous wp_head functions by calling do_action('my_wp_head')
    4. Process output with a call to echo preg_replace( array_keys($patterns), array_values($patterns), ob_get_clean() );
share|improve this answer
I would love to see this as something I could use in functions.php. – jnthnclrk Aug 5 '13 at 15:49
@jnthnclrk As you might have read on the help center, "do this job for me questions are out of scope". Even with bounty attached. – kaiser Aug 12 '13 at 10:13
Ah, come on. That's no fun! – jnthnclrk Aug 14 '13 at 20:50
So fwiw, you can use that in functions.php, however since I'm an IT consultant it wouldn't make much sense for me to just do it for you... I wouldn't want to deprive you of the learning experience :P – doublesharp Aug 14 '13 at 23:44
I have tried and failed. – jnthnclrk Aug 22 '13 at 17:08

Use http://wordpress.org/plugins/wp-minify/. It will merge css, js & html + remove useless comments. Your page speed will be improved, and all HTML code in the source will be compacted.

Problem solved, I guess!

share|improve this answer
Not really I'm looking for a clean function filter to add tabulation to wp_head(). – jnthnclrk Aug 7 '13 at 9:56

You can solve it with creating a custom function inside "functions.php":

function indented_wp_head(){
    $header = ob_get_contents();
    echo preg_replace("/\n/", "\n\t", substr($header, 0, -1));
    echo "\n";

Now you just need to call this function in the "header.php":

    <!-- ... -->
    <?php indented_wp_head() ?>

If you want to use it for wp_footer() too, you can create this function for general usage in "functions.php":

 * Indents the output of
 * a function
 * @return void
if (! function_exists("print_indented")) {

    function print_indented($fn)
        $html = ob_get_contents();
        echo preg_replace("/\n/", "\n\t", substr($html, 0, - 1));
        echo "\n";

And call it in "header.php" or "footer.php" like

<?php print_indented("wp_footer") ?>
<?php print_indented("wp_head") ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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