4

I've got a simple header file which calls the wp_head method, however when i inspect the source the css links are made in the body and the <head> is empty.

My header.php:

<!DOCTYPE html>
<html lang="en">

<head>
<?php wp_head(); ?>
</head>
<body>
<?php get_template_part( 'components/navigation/navigation', 'main' ); ?>

My functions.php:

<?php 
function load_scripts() {
    echo ' test';
    wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action('wp_enqueue_scripts', 'load_scripts');

?>

Thanks!

1 Answer 1

6

This is your browser's attempt to avoid invalid markup. Your echo ' test'; outputs something that does not belong in a head tag, so the browser closes it and opens the body tag. If you look at the actual unprocessed response from the server, it won't appear this way, but that echo shouldn't be there regardless, and this issue will disappear when you remove it.

5
  • Hi, Thanks for your answer, however, after removing the echo statement it still comes out in the body.
    – Niek
    Commented May 12, 2018 at 17:04
  • Then you have something else outputting something other than a valid tag in your head.
    – Milo
    Commented May 12, 2018 at 17:06
  • This legit helped me track down the issue I was also having. Thx!
    – Gurnzbot
    Commented Dec 12, 2019 at 18:06
  • I was using an echo statement to display debug info on the screen BEFORE I was outputting the meta tag and it was showing up in the body. Once I removed my debug output, the meta properly showed up in head section!
    – jsherk
    Commented Feb 6, 2020 at 2:23
  • @Milo Thanks, you saved my time. Such a silly and simple cause... To look for the cause I've started debugging the plugins' 'wp_head' hooks. :) Commented Sep 3 at 12:36

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.