How to Add Code to the WordPress Header and Footer

How to Add Code to the WordPress Header and Footer

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.

PlaceWhat goes thereWhy
Inside <head>Verification tags, fonts, consent, stylesThe browser needs it before drawing
Right after <body>The second part of Tag ManagerIt asks for it
Before </body>Chats, widgets, most scriptsIt 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.

PluginBest forInstalls, rating (reviews)
WPCodeHeader, footer and PHP snippets3 million+
4.9 (1,938)
Header Footer Code ManagerChoosing pages one by one600,000+
4.9 (309)
Head & Footer CodeA light, simple option100,000+
5.0 (51)

With WPCode:

  1. Install it and open Code Snippets → Header & Footer.
  2. Paste the code in the box for the header, the body or the footer.
  3. Save, and clear your site's cache.
  4. 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

  1. Only code from a source you trust, copied from its own panel. A script can read what your visitors type.
  2. One thing at a time, and note the date and the reason.
  3. async or defer on scripts, if the service allows it.
  4. No duplicates. Two Analytics tags double your visits.
  5. Remove what you no longer use. A chat you canceled two years ago is still loading.
  6. 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() or wp_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.

Related Articles

WordPress Accessibility: EAA & WCAG 2.2 Checklist WordPress Tutorials

WordPress Accessibility: EAA & WCAG 2.2 Checklist

WordPress accessibility checklist for the EAA and WCAG 2.2: fix contrast, keyboard, forms and checkout issues. Start auditing your site today.

Nov 23, 2026
WordPress Plugin Conflicts: Find & Fix Them WordPress Tutorials

WordPress Plugin Conflicts: Find & Fix Them

WordPress plugin conflicts: find the cause and fix it fast with this step-by-step guide. Try Phluit hosting.

Nov 9, 2026
WordPress Maintenance Plan: What Agencies Should Include WordPress Tutorials

WordPress Maintenance Plan: What Agencies Should Include

WordPress maintenance plan essentials for agencies: updates, backups, security. Build yours and explore Phluit hosting today.

Nov 2, 2026