Indenting (tabbing) WP_head

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?

Read More

Thanks

Related posts

Leave a Reply

5 comments

  1. 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() );
  2. 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/", "nt", 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/", "nt", 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") ?>
    
  3. 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</", "nt<", substr($header, 0, -1));
      echo "n";
    }
    
  4. 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'); ?>