Sooner or later some service asks you to "paste this code before </head>": statistics, a chat, the verification of Search Console, an advertising pixel. WordPress has no box for that, so you need to know where it goes.
There are four good ways. The worst one, which many tutorials still recommend, is editing header.php of the theme: the next update deletes it.
Header, body or footer?
| Place | What goes there | Why |
|---|---|---|
Inside <head> | Verification tags, fonts, consent, styles | The browser needs it before drawing |
Right after <body> | The second part of Tag Manager | It asks for it |
Before </body> | Chats, widgets, most scripts | It doesn't delay the page |
If the service doesn't say, use the footer. Whatever is in the header delays what the visitor sees.
Method 1: a plugin
The safest for most people. The code is saved in the database, survives changes of theme, and is removed with one click.
| Plugin | Best for | Installs, rating (reviews) |
|---|---|---|
| WPCode | Header, footer and PHP snippets | 3 million+ 4.9 (1,938) |
| Header Footer Code Manager | Choosing pages one by one | 600,000+ 4.9 (309) |
| Head & Footer Code | A light, simple option | 100,000+ 5.0 (51) |
With WPCode:
- Install it and open Code Snippets → Header & Footer.
- Paste the code in the box for the header, the body or the footer.
- Save, and clear your site's cache.
- Open the page in a private window and press Ctrl+U: your code must be there.
To load something only on certain pages, create a snippet instead of using the general boxes and choose its location. Header Footer Code Manager is very clear for that; its last update was in July and it was tested up to WordPress 7.0. Head & Footer Code is the lightest of the three and lets you add code to a single post.
If you only need Google Analytics, Search Console and AdSense, Site Kit by Google connects them without pasting anything. On Phluit's WordPress plans, Insightor does the same for Analytics and Tag Manager, with the statistics in your dashboard.
Method 2: a child theme
If you are comfortable with code, use WordPress hooks in the functions.php of your child theme. They don't depend on the theme's templates.
add_action( 'wp_head', function () {
?>
<meta name="google-site-verification" content="YOUR-CODE">
<?php
} );
add_action( 'wp_footer', function () {
if ( is_page( 'contact' ) ) {
?>
<script src="https://example.com/chat.js" async></script>
<?php
}
}, 20 );For right after <body> there is the wp_body_open hook.
For your own files, the right way is to enqueue them. That way WordPress controls the order and doesn't load them twice:
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script(
'my-script',
get_stylesheet_directory_uri() . '/js/my-script.js',
array(),
'1.0',
array( 'strategy' => 'defer', 'in_footer' => true )
);
} );An error in this file can leave the site blank. Have SFTP access at hand, or try it on a staging copy.
Method 3: the site editor
In block themes, Appearance → Editor → Patterns → Template parts lets you open the header or the footer and add a Custom HTML block. It serves for things that are seen: a badge, a form, a notice.
It doesn't serve for <head>, since the editor only touches the visible part. And some users, depending on their role, can't save scripts there.
Method 4: Google Tag Manager
If you add and remove marketing tags often, install Tag Manager once (with method 1) and manage the rest from its panel: Analytics, pixels, conversions. You get versions, a test mode and rules about when each one is triggered.
It also has a risk. Whoever controls that account can run any code on your site. Give access to few people, with two-step sign-in.
Examples
Verifying Search Console
Copy the meta tag and put it in the header. Better still, verify by DNS: it doesn't depend on the site.
Google Analytics 4
Put the two parts of the tag in the header. If you have visitors from Europe, they must load after consent, or with consent mode.
A live chat
In the footer, on all pages except the checkout. Here are the chat plugins.
A bit of CSS
You don't need any of this. Use Appearance → Customize → Additional CSS, or Styles → Additional CSS in the site editor.
Rules for not getting into trouble
- Only code from a source you trust, copied from its own panel. A script can read what your visitors type.
- One thing at a time, and note the date and the reason.
asyncordeferon scripts, if the service allows it.- No duplicates. Two Analytics tags double your visits.
- Remove what you no longer use. A chat you canceled two years ago is still loading.
- Check the speed before and after, with our guide to Core Web Vitals.
When it doesn't appear
- Cache. Clear the plugin's, the server's and the CDN's.
- You are signed in. Many plugins hide statistics from administrators. Use a private window.
- The theme doesn't call
wp_head()orwp_footer(). It happens with very old or handmade themes. - An optimizer joins or delays scripts. Exclude yours.
- A security policy blocks it. Press F12 and read the console: it says "Content Security Policy".
If you would rather someone else did it, small changes like these are part of what our development team does.
Frequently asked questions
Can I paste PHP in the header box?
No: those boxes are for HTML, CSS and JavaScript. For PHP, use a snippet in WPCode or your child theme.
Will I lose the code if I change theme?
With a plugin, no. In a child theme, yes: it belongs to that theme.
Does it slow down my site?
A line or two, no. What weighs is the script that gets loaded. Count how many outside services each page calls.