Adding custom code to your WordPress header or footer is a task most site owners will eventually need to tackle — whether it’s embedding Google Analytics, adding a verification meta tag, or loading a third-party script. WordPress doesn’t give you direct access to these areas by default, but there are two reliable ways to do it.
In this guide, I’ll walk you through both methods: using a dedicated plugin (the easiest approach) and editing your theme’s functions.php file directly (for those comfortable with code).

How to Add Header & Footer Code in WordPress
Before diving in, it’s worth understanding why the header and footer matter — and why you’d need to add code there rather than in the body of a page.
Every web page is structured into three sections:
- Header (the <head> section) — Loads before anything visible on the page. This is where meta tags, CSS stylesheets, analytics tracking codes, and scripts that need to initialise early belong.
- Body — The visible content of your page. Most of what you create in WordPress lives here.
- Footer (just before </body>) — Loads last, after the page content. JavaScript that doesn’t need to run early is better placed here, as it improves page load performance.
Servers load pages in a linear sequence — head, then body, then footer. This means header code initialises first, while footer code runs after everything else on the page has loaded. Understanding this helps you decide where each script belongs.
Common reasons to add header or footer code in WordPress include:
- Installing Google Analytics 4 or Google Tag Manager
- Adding Google Search Console site verification
- Embedding Facebook Pixel or other ad tracking scripts
- Adding heatmap tools like Microsoft Clarity (free in 2026)
- Loading custom CSS or JavaScript globally
- Connecting SEO tools that require a verification snippet in the <head>
In a standard WordPress setup, you don’t have direct access to the header and footer through the dashboard. You’ll need one of the two methods below to do it properly.
Method 1: Use a Plugin (Recommended)
For most WordPress users, a plugin is the simplest and safest way to add header and footer code. You don’t need to touch any theme files, and the code persists even if you update or switch themes.

Two solid options in 2026:
- Insert Headers and Footers by WPCode — The most popular and well-maintained option. Lets you add global scripts to the header and footer, with the option to target specific pages or post types.
- Embed Code — A lightweight alternative with a similar feature set, good for simpler setups.
- Go to Plugins > Add New in your WordPress dashboard
- Search for Insert Headers and Footers and install the WPCode version
- Activate the plugin
- Navigate to Code Snippets > Header & Footer in your dashboard menu
- Paste your code into the Header box (for scripts that load early) or the Footer box (for scripts that can load later)
- Click Save Changes

The code will now be injected globally across your site — in the <head> section or just before the closing </body> tag, depending on which box you used.
If you only need to add code to a specific page or post type rather than sitewide, the plugin supports that through snippet conditions — you can restrict it to specific URLs, post types, or user roles.

Pro Tip Always use a plugin rather than editing your theme’s header.php directly. If your theme updates, any direct file edits will be overwritten. A plugin keeps your changes safe regardless of what happens to your theme. If you’re running a website maintenance routine, managing these snippets through a plugin is much easier to track long-term.
Method 2: Edit functions.php
If you prefer to work directly with WordPress code — or you’re building a custom theme — you can use WordPress action hooks to inject code into the header and footer without a plugin. This is the approach used in professional WordPress development.
Before you start, you’ll need:
- Access to your site’s files via SFTP or your hosting file manager
- Basic familiarity with WordPress hooks and PHP
- A child theme — strongly recommended so your changes survive theme updates
How to Add Code via functions.php
- Connect to your server via SFTP or use your hosting control panel’s file manager
- Navigate to wp-content/themes/your-child-theme/
- Open functions.php in a code editor
- Add the following snippet at the bottom of the file:
// Add code to the WordPress header
add_action( 'wp_head', 'my_custom_header_code' );
function my_custom_header_code() {
?>
<?php
}
- Replace the comment placeholders with your actual code (e.g. your GA4 script or GTM snippet)
- Save the file and test your site immediately — a PHP syntax error in functions.php can take your site offline
The wp_head hook fires inside the <head> section of every page. The wp_footer hook fires just before the closing </body> tag. Both are standard WordPress hooks — you can learn more about them in the WordPress Developer hook reference, specifically the wp_head and wp_footer entries.
A Note on Child Themes
Never edit the functions.php of a parent theme directly. When the theme updates, your changes will be overwritten and lost. A child theme inherits all the parent theme’s functionality while keeping your customisations safe.
If you don’t have a child theme in place, plugins like Child Theme Configurator can create one in seconds at no cost.
Which Method Should You Use?
| Scenario | Recommended Method |
|---|---|
| You want the simplest solution | Plugin (WPCode) |
| You’re not comfortable editing PHP files | Plugin (WPCode) |
| You need to add code to specific pages only | Plugin (WPCode with conditions) |
| You’re building a custom or bespoke theme | functions.php via child theme |
| You want to minimise plugin count | functions.php via child theme |
Summary
Adding header and footer code in WordPress comes down to two solid approaches. For most people — especially those who aren’t developers — a plugin like WPCode is the right choice. It’s simple, safe, and gives you full control without touching any files.
If you’re comfortable with PHP and working with WordPress themes, the functions.php approach using wp_head and wp_footer hooks is equally reliable and keeps your site lean. Just make sure you’re always working in a child theme.
Either way, once your code is in place, verify it’s loading correctly by checking your page source (right-click > View Page Source) and searching for your snippet. If you need help with your WordPress site, feel free to get in touch.

With over two decades of web design and development expertise, I craft bespoke WordPress solutions at FallingBrick, delivering visually striking, high-performing websites optimised for user experience and SEO.


