0

I've had this question for a long time and I was wondering if anyone had an answer for it. I know its not really important, but for someone like me who really likes very clean markup things like this are a realy pain...

So this is my header:

enter image description here

As a sidenote, you can see its nice and minimal as I've removed the markup that Wordpress automatically places in the header.

The red outlined section is everything that is generated in functions.php and as you can see it has no tab spaces besides the first line...

enter image description here

I know it's really trivial, but we are shooting to do the best job possible and I would really like to get this sorted. Does anyone know how to add the tab spaces?

In case it is important here is the finctions.php code:

function thx_javascript_detection() {
    echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
}
add_action( 'wp_head', 'thx_javascript_detection', 0 );

/**
 * Enqueue scripts and styles.
 */
function thx_scripts() {  

    // Theme stylesheet.
    wp_enqueue_style( 'thx-style', get_stylesheet_uri() ); 

    // Load the Internet Explorer 8 specific stylesheet.
    wp_enqueue_style( 'thx-ie8', get_theme_file_uri( '/assets/css/ie8.css' ), array( 'thx-style' ), '1.0' );
    wp_style_add_data( 'thx-ie8', 'conditional', 'lt IE 9' );

    // Enqueue jQuery.
    wp_enqueue_script( 'jquery');

    // Font Awesome Script.
    wp_enqueue_script( 'font-awesome', 'https://use.fontawesome.com/41d213027b.js');
}
add_action( 'wp_enqueue_scripts', 'thx_scripts' );

P.S. this site is in the early satges still so some things (like tagline, lol) will be changing.

1 Answer 1

2

I know it's really trivial

Yes. Yes it is.

...we are shooting to do the best job possible

Spending any time whatsoever on fixing tab indentation in your output HTML as opposed to focusing on the quality of your source code is not doing "the best job possible". You should be minifying your HTML on-the-fly anyway!

But for the sake of answering a question, you could use the style_loader_tag and script_loader_tag filters:

function wpse_248549_tag_indent( $tag ) {
     return "\t\t$tag";
}

add_filter( 'style_loader_tag',  'wpse_248549_tag_indent' );
add_filter( 'script_loader_tag', 'wpse_248549_tag_indent' );

However, this won't handle other output that might be attached to wp_head - you'll either have to use the same approach for other hooks (if available), or you could use a dirty bit of output buffering:

add_action( 'wp_footer', 'get_in_line::open', 1    );
add_action( 'wp_footer', 'get_in_line::stop', 1000 );
add_action( 'wp_head',   'get_in_line::open', 1    );
add_action( 'wp_head',   'get_in_line::stop', 1000 );

class get_in_line {
    /**
     * Start the output buffer.
     */
    static function open() {
        ob_start();
    }

    /**
     * Get the contents of the buffer and spring clean.
     */
    static function stop() {
        $lines = [];

        foreach ( preg_split( '/[\n\r]+/', ob_get_clean() ) as $line ) {
            if ( trim( $line ) !== '' ) {
                if ( strpos( $_line = ltrim( $line ), '<' ) !== 0 ) {
                    $_line = $line;
                }

                $lines[] = $_line;
            }
        }

        echo "\t\t" . implode( "\n\t\t", $lines ) . "\n";
    }
}
1
  • thanks, I agree with you when it comes to minifying HTML... will have a think about it, but thanks for answering the question regardless. Your answer worked great for eveything except the title tag. Any ideas why?
    – thebigtine
    Commented Dec 7, 2016 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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