2

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?

Thanks

  • 2
    Why does this matter? – Wyck Aug 5 '13 at 23:23
  • 1
    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
2

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() );
  • 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
2

A better way, to avoid indenting lines that are already indented which some seem to be

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

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

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

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

<head>
    <!-- ... -->
    <?php indented_wp_head() ?>
</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)
    {
        ob_start();
        call_user_func($fn);
        $html = ob_get_contents();
        ob_end_clean();
        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") ?>
0

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!

  • Not really I'm looking for a clean function filter to add tabulation to wp_head(). – jnthnclrk Aug 7 '13 at 9:56
0

As additions we can specify the number of tabs for html blocks that need more than one tab and use parameters for the functions.

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

Before

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

After:

<?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.