Add Header and Footer Code in WordPress: 4 Easy Ways
Yes, you can easily add header and footer code in WordPress using a plugin, your theme’s functions.php file, or a child theme. The most beginner-friendly method is installing a free plugin like “Insert Headers and Footers,” which gives you a simple text area for each location. This article walks through all the main options, what each one does, and how to avoid common pitfalls.
Why You Might Need Header and Footer Code
Header and footer code refers to snippets that need to run on every page of your site, not just one post. The most common examples are:
- Analytics scripts – Google Analytics, Matomo, or Plausible tracking code
- Meta tags – verification codes from Google Search Console, Bing Webmaster Tools, or Pinterest
- Fonts and CSS – Google Fonts links, custom CSS that must load early
- Third-party widgets – live chat widgets, cookie consent banners, or pixel tracking from ad platforms
- Schema markup – JSON-LD structured data for rich results
Most of these snippets are provided by the service as a block of HTML or JavaScript that you paste into your site. The header is the <head> section, which loads before the page content. The footer is the </body> area, which loads after everything else. Some scripts must be in the header (like Google Analytics), while others work fine in the footer (like many chat widgets).
Method 1: Using a Plugin (Easiest for Beginners)
Plugins are the safest and most user-friendly way to add header and footer code, especially if you are not comfortable editing theme files. Here is how it works with a popular free plugin like “Insert Headers and Footers” by WPBeginner:
- In your WordPress admin, go to Plugins → Add New.
- Search for “Insert Headers and Footers” and install it.
- After activation, you will see a new menu item called WPCode (the plugin was rebranded). Click on Header & Footer.
- Paste your header code into the Header box and your footer code into the Footer box.
- Click Save Changes.
The plugin outputs the code exactly as you paste it. It does not modify your theme files, so your changes survive theme updates. Most similar plugins work the same way: they add a settings page where you paste code, and they output it on every page using WordPress hooks like wp_head and wp_footer.
Method 2: Editing functions.php (More Control)
If you prefer not to use a plugin, you can add code directly to your theme’s functions.php file. This gives you full control and avoids an extra plugin, but it requires a bit more care. Here is a typical snippet:
// Add code to the header
function my_custom_header_code() {
?>
<!-- Paste your header code here -->
<?php
}
add_action('wp_head', 'my_custom_header_code');
// Add code to the footer
function my_custom_footer_code() {
?>
<!-- Paste your footer code here -->
<?php
}
add_action('wp_footer', 'my_custom_footer_code');
To add this, go to Appearance → Theme File Editor, select functions.php from the right-hand list, and paste the code at the very end (before the closing ?> if there is one, or just after the last line). Then click Update File.
Important: Always use a child theme if you edit functions.php directly. If you edit the parent theme, your changes will be lost when the theme updates. A child theme is a separate theme that inherits all the parent’s functionality but lets you safely add your own code. Most WordPress hosts allow you to create a child theme by uploading a folder with two files: style.css and functions.php.
Method 3: Using a Theme’s Built-in Options
Many WordPress themes now include a dedicated section for header and footer scripts. For example, themes like Astra, GeneratePress, and Kadence have a “Custom Scripts” or “Code” panel in the Customizer or theme settings. You simply navigate to Appearance → Customize → Additional Code (or similar) and paste your scripts there.
This method is just as safe as using a plugin because the theme stores the code separately from the main template files. However, it is theme-specific, so if you switch themes, you will need to copy the code to the new theme’s settings.
Method 4: Using a Site-Specific Plugin (Best for Maintenance)
If you want to keep your code even after switching themes, you can create a simple “site-specific” plugin. This is a tiny plugin that only contains your custom code. Here is how:
- Create a folder in
wp-content/plugins/calledmy-custom-code. - Inside that folder, create a file called
my-custom-code.php. - Add the following header and your custom functions:
<?php
/*
Plugin Name: My Custom Code
Description: Adds header and footer scripts site-wide.
*/
function my_custom_header() {
?>
<!-- Header code -->
<?php
}
add_action('wp_head', 'my_custom_header');
function my_custom_footer() {
?>
<!-- Footer code -->
<?php
}
add_action('wp_footer', 'my_custom_footer');
Then go to Plugins → Installed Plugins and activate “My Custom Code.” This method is more advanced but keeps your code independent of any theme, making it ideal for developers who maintain multiple sites.
Common Mistakes and How to Avoid Them
Adding header and footer code is straightforward, but a few errors can break your site or cause issues:
- Not using a child theme – Editing the parent theme’s
functions.phpmeans your code disappears on the next theme update. Always use a child theme. - Pasting code inside another script tag – If you paste a full
<script>...</script>block inside a code area that already wraps it in script tags, you will get nested tags. Most plugins and theme options expect the raw JavaScript without the surrounding tags. - Forgetting to test on a staging site – A syntax error in
functions.phpcan cause a white screen (the “fatal error”). If that happens, you can access your site via FTP and delete or fix the code. - Adding code to the wrong location – Some scripts need to be in the header (e.g., analytics that track page views) and some in the footer (e.g., chat widgets that should load last). If a script doesn’t work, try moving it to the other location.
- Not clearing cache – If you use a caching plugin, your new code may not appear immediately. Clear the cache after adding code.
How to Choose the Right Method for You
Your choice depends on your comfort level and how long you plan to keep the code:
- Beginner or non-technical user – Use a plugin like “Insert Headers and Footers.” It is free, safe, and easy to remove.
- Intermediate user who wants no extra plugin – Use your theme’s built-in code area if it has one, or create a child theme and edit its
functions.php. - Developer or site with many custom snippets – Use a site-specific plugin or a code snippet manager plugin like “WPCode” (which also lets you conditionally load code on specific pages).
If you only need to add code to a single page or post, you can do that without any header/footer method: edit the page, switch to the HTML/Code editor, and paste the script directly into the content area. However, that only works for that one page, not site-wide.
Frequently Asked Questions
Will adding header code slow down my site?
It depends on the code. A small analytics snippet has negligible impact. Large scripts, multiple external requests, or render-blocking CSS in the header can slow down your site. To minimize impact, use the footer for scripts that don’t need to run early, and consider deferring JavaScript where possible.
Can I add header and footer code without a plugin?
Yes. You can edit your theme’s functions.php file using the built-in theme editor, or create a child theme and add the code there. This requires a basic understanding of PHP and WordPress hooks.
What if my code breaks the site?
If you edited functions.php and get a white screen, you can fix it by accessing your site via FTP or your hosting file manager. Open functions.php and remove or correct the code you added. If you used a plugin, simply deactivate the plugin via the admin dashboard (if you can still access it) or via FTP by renaming the plugin folder.
How do I add code to only a specific page?
Use a snippet manager plugin like WPCode, which allows you to set conditional logic (e.g., “show on page ID 42”). Alternatively, you can paste the code directly into that page’s HTML editor, but that only works for the content area, not the <head> section.
Is it safe to paste Google Analytics code in the header?
Yes. Google Analytics provides a JavaScript snippet that is designed to be placed in the <head> of your site. Using a plugin or theme option is the safest way because it keeps the code separate from your content and easy to remove if needed.
What is the difference between wp_head and wp_footer hooks?
wp_head is a WordPress action hook that runs inside the <head> section of your theme. wp_footer runs just before the closing </body> tag. You can attach your custom functions to these hooks to output code in those locations.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.