Back to Blog

How to Add Google Analytics, Tag Manager and Other Code to the WordPress Header and Footer

Add Google Analytics, Tag Manager, Meta Pixel and verification tags to the WordPress head or footer without editing theme files — and which plugin to use.

To add Google Analytics, Tag Manager or other code to WordPress without editing theme files, use a header and footer plugin. In the free ExtenderX plugin, open Custom Code, add a snippet, paste the code and choose Header, Body Open or Footer. It survives theme updates, works on block themes, and you can switch any snippet off without deleting it.

Why not edit the theme?

  • Theme updates overwrite header.php and footer.php.
  • Switching themes silently drops your tracking.
  • Block themes don't have those files at all.
  • One typo in a PHP template can take the site down.

A plugin keeps your code separate and lets you toggle it.

Add a snippet with ExtenderX

  1. Install and activate ExtenderX.
  2. Go to ExtenderX → Custom Code and click Add Snippet.
  3. Name it (for example "GA4") and paste the code.
  4. Choose where it goes:
    • Header — inside <head>
    • Body Open — right after <body>
    • Footer — before </body>
  5. Save. Disabled snippets stay saved but stop running.

Where common codes go

CodeLocation
Google Analytics (gtag.js)Header
Google Tag Manager — scriptHeader
Google Tag Manager — noscriptBody Open
Meta (Facebook) PixelHeader
Google AdSense auto adsHeader
Site verification meta tag (Google, Bing, Pinterest)Header
Chat widgetsFooter
Custom CSSHeader (or a CSS snippet)

After saving, check the code is live with the service's own tester — Google Tag Assistant, Meta Pixel Helper, or the verification button in Search Console.

Which plugin should you use?

ExtenderXWPCodeHeader Footer Code ManagerCode Snippets
Header / footer scripts, freeYes (+ Body Open)YesYes (+ before/after content)— (HTML via shortcodes)
CSS & JavaScript snippets, freeYesYesYesPro
PHP snippets, freeProYes—Yes
Load on specific pages, freeProYes (smart conditions in Pro)YesPro
Snippet library—Yes—Community cloud
Also an accessibility toolbar, dark mode, animations, maintenance modeYes———

Sources: WPCode, Header Footer Code Manager, Code Snippets, as of October 25, 2026. Disclosure: WPXERO makes ExtenderX (version 1.10.0).

Our honest take: if code is all you need, and especially if you write PHP snippets, a dedicated code plugin does more for free. If you'd otherwise install separate plugins for tracking codes, an accessibility toolbar, dark mode, maintenance mode and block animations, ExtenderX replaces all of them with one dashboard — and a disabled feature adds zero CSS and JavaScript.

Pro: PHP, conditions and scheduling

ExtenderX Pro adds PHP snippets, conditional display for specific pages or post types, and scheduling, starting at $19.99 a year.

Keep your scripts tidy

  • Remove what you don't use. Old pixels from past campaigns still load on every page.
  • Use one tag manager instead of five separate tracking codes.
  • Check your consent setup. If you need a cookie banner, tracking scripts should respect it.
  • Label every snippet so you know what it does a year from now.

Common Questions

Use a header and footer plugin. In ExtenderX, go to ExtenderX → Custom Code, click Add Snippet, paste the code, choose Header, Body Open or Footer as the location and save. The code survives theme updates and theme changes.

Google's gtag.js snippet goes in the head section, so choose Header. Google Tag Manager has two parts — the script goes in the head and the noscript part right after the opening body tag, so use Header and Body Open.

Theme updates overwrite header.php and switching themes loses the code. Block themes don't have a header.php at all. A plugin keeps the code separate from the theme.

ExtenderX's free version handles HTML, CSS and JavaScript; PHP snippets, conditional display and scheduling are part of ExtenderX Pro. Code Snippets and WPCode also run PHP snippets in their free versions.

In ExtenderX, conditional display by page or post type is a Pro feature. Header Footer Code Manager also offers page, category and device targeting for free.

Every third-party script adds some weight. Add only what you use, remove old tracking codes, and prefer the footer for scripts that don't need to load first.

Get new guides and releases by email

Occasional emails when we publish a guide or ship a notable plugin update. No spam — unsubscribe any time.

We only use your email to send these updates. See our privacy policy.