I really like having my code all properly indented and all, but all the code generated by wordpress, like the one that appears on the header when we use wp_head(), loses all the indentation.

I think it doesn't really matter, neither the users nor the search engines give a s**t about the code being indented or not, but when it comes to debugging, it really helps a lot.

Do you know of any plugin or option or something to fix this?

2 Answers 2


I don't think correct global indentation is possible without massive (and inefficient) output buffering to change it.

Since you are primarily interested in this for debug purposes I suggest using some tool that will format and color-code output (of any page btw, not just those that you control).

Firebug and View Source Chart work nicely for me.

  • thanks, I didn't knew this plugin, thanks! He's right though, as WordPress doesn't separate html from php it's almost impossible without rewriting a whole bunch of code. I've made some minor attempts located here klanten.corvannoorloos.com/html5, but it has made me rewriting a whole lot of code (starting with almost every widged)
    – user5424
    Jan 31, 2012 at 19:57

You can use a custom helper function in functions.php

function add_indented($fn, $num_tabs=1, $params=null){
    call_user_func($fn, $params);
    $html = ob_get_contents();
    for ($i=0 ; $i<$num_tabs ; $i++) $tabs.="\t";
    echo preg_replace("/\n/", "\n" . $tabs, substr($html, 0, - 1));
    echo "\n";

Then we use this function where we need to add the tabs.


<?php wp_head(); ?>
<?php wp_footer(); ?>
<?php get_template_part('template-parts/content','home'); ?>


<?php print_indented("wp_head"); ?>
<?php print_indented("wp_footer"); ?>

<!-- Add 4 tabs -->    
<?php print_indented('get_template_part', 4,'template-parts/content', 'home'); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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