WordPress function: wp_head

I was recently working on optimizing a site with Yoast’s WordPress SEO plugin (it’s the only SEO plugin in my books), but my optimized titles – <title> – and meta descriptions weren’t being updated on the site. I’d enter the content for the Page or Post, click Update, and the fields would reset to the site’s default.

After searching for the solution for a while, I finally found the problem: the wp_head template tag. Not that it wasn’t there, it just wasn’t in the right place.

Location, location, location

At first I was really stumped because the site had all the critical hooks, like wp_head and wp_footer (I’ve written here in the past about how critical these hooks are). But I learned something new – with the wp_head function, it’s not just about having it, it’s about where it’s located.

Turns out wp_head must be placed right before the closing </head> tag, which is generally located in the header.php file of a theme:

Put this template tag immediately before </head> tag in a theme template (ex. header.php, index.php).

In this particular site, the Google Analytics tracking code had been stuck in the <head> between wp_head and </head>. I moved the tracking code above wp_head, and the WordPress SEO plugin started to work.

So there you have it – if a plugin isn’t working, check that your theme not only has wp_head, but that it’s also in the right place.