How to insert code into the header of a WordPress post or page

Sometimes you need to insert code into the header of pages on your website, such as Google Analytics code, Pinterest site verification code and so on. The easy way is with a plugin. Here’s how.

Web page headers

Web pages have two parts to them and they have a head and a body. The body contains the bits you can see, such as the text, images, menus and links you can see on this page.

The head is a section right at the start, before the body, and it contains lots of code that is important and has various purposes, but which you don’t need to see or access. The HTML for a web page looks like this:

<html>
 <head>
  Lots of code...
 </head>
 <body>
  The web page contents...
 </body>
</html>

Some WordPress plugins add code or scripts to the head section of web pages and a good example is an SEO plugin like Yoast or All-in-One SEO. They add meta tags that describe the contents of the page for search engines indexing your site.

Some web services require code to be added to the head or header section and Google Analytics code must be present in order to track visitor numbers for example. Sometimes advertising code needs to be added to the head section and Google AdSense has a couple of scripts which need to be added. Pinterest and other site verification codes must be added to the head section.

So how do you do it? The easy way is with a plugin.

All-in-One SEO

This is a great plugin that is useful not only for SEO, but for its many other features too. One of the extras features that is included is an easy way to insert extra code into the header of web pages on your site.

Go to All-in-One SEO in the WordPress sidebar and in General Settings, towards the bottom of the page, are these boxes. You can place code in the header for posts, pages and the home page.

All-in-One SEO options to insert code into the header

Just copy the code from the source, click in the boxes and press Crl+V or Cmd+V to paste it in. Scroll down to the bottom and click Update Options.

You could put Google Analytics code here, but there is a separate section for that. In fact, you don’t even need to enter the code, just your Google Analytics ID.

Insert Google Analytics code into WordPress with All-in-One SEO

There are also separate sections for Google and Bing webmaster verification and Pinterest verification. You just paste the code given by these services into the boxes.

Webmaster and Pinterest verification in WordPress with All-in-One SEO

This is one of the reasons I prefer All-in-One SEO to Yoast – it has extras that saves you having a bunch of other plugins. Yoast does have webmaster verification, but not Pinterest and can’t insert extra code into the header.

Insert Headers and Footers

If you do not already have a plugin to insert code into the header, Insert Headers and Footers plugin is a popular choice. It adds a menu to the Settings section of the WordPress sidebar and its best feature is its simplicity.

Insert Headers and Footers WordPress plugin

There are two boxes, one to insert into the head section and another to insert just before the end of the body (rarely needed). Just paste in the code and save it. There is nothing to it and it is recommended.

Header and Footer scripts

Header and Footer Scripts plugin is identical. In fact, I am not even going to show a screenshot because it is so similar. If it wasn’t for the name, you would think it was the same plugin.

There are just two boxes, one for code you want to insert into the head, and another for the footer (less used). It is easy to use and is therefore recommended.

Head, Footer and Post Injections

Head, Footer and Post Injections is a plugin for expert users and it is a lot more complicated, so avoid it if you want to keep things simple.

The plugin is able to inject code into not just the header, but in numerous places throughout pages and posts. For example, it inserts after the opening body tag, before the closing body tag, before and after the post content, with separate code for desktop and mobile, inside posts and more.

It is possible to do some advanced tricks with this plugin, but avoid it unless you are an expert.