How To Add Header And Footer Code In WordPress (the Easy Way)

At WPBeginner, we create lots of tutorials that require you to insert code into the <header> or <footer> sections of your WordPress site. For many users, this can feel intimidating, especially since WordPress doesn’t have a built-in way to insert code in these areas.

However, adding code to your header and footer is essential for integrating popular services like Google Analytics, Google Search Console, Facebook Pixel, and more. You might also need to add custom CSS or JavaScript as part of other WordPress tutorials.

The good news? You don’t have to edit your theme files directly or worry about breaking your site. Using the free WPCode plugin, even beginners can safely and easily add code snippets to their header or footer.

In this guide, we’ll walk you through how to add header and footer code in WordPress, step-by-step.

How to Add Header and Footer Code in WordPress

The Best Way to Add WordPress Header and Footer Code

If you want to add WordPress header and footer code, there are three possible solutions:

  1. Manually, by editing your theme’s header.php and footer.php files
  2. With your theme’s built-in header and footer code feature
  3. Using a headers and footers WordPress plugin

The first option is not beginner-friendly because it requires you to add header and footer code by directly editing the header.php and footer.php files manually.

Another disadvantage of using this method is that your code will be removed if you install an update to your theme.

The second option is using your theme’s built-in feature. Some WordPress themes like Elegant Themes offer a built-in option for quickly adding code and scripts to your WordPress header and footer.

If you are using a theme with a built-in feature, then it seems like a safe and simple solution. However, if you change your theme, then all the code snippets added to your website will be gone. This includes site verification in Google Search Console, website analytics via Google Analytics, and so on.

That’s why we always recommend users to use the third option, a headers and footers plugin. This option is by far the easiest and safest method to add header and footer code in WordPress.

After a lot of requests from readers, our team has built the WPCode plugin.

WPCode

WPCode (formerly called Insert Headers and Footers) is a 100% free code snippet plugin. You can use it to easily add code to the header and footer in WordPress.

Here are some benefits of using the WPCode plugin:

1. 🚀 Easy, Fast, and Organized: It lets you add code to your site’s header and footer easily and quickly. Plus, it keeps you organized by allowing you to store all your footer and header codes in one place.

2. ✅ Prevents Errors: The smart code snippet validation helps prevent errors that may occur if you edit your theme files manually.

3. 😌 Upgrade or Change Your Theme Without Worry: The plugin will save your header and footer code in a separate place, so you can update or change your theme without worrying about the code being erased.

Aside from header and footer scripts, you can also use WPCode to easily insert custom PHP, JavaScript, CSS, HTML, and text code snippets without editing your theme files.

Plus, WPCode has a built-in snippets library where you can find all the most useful WordPress code snippets. This allows you to quickly remove WordPress features that you don’t want like automatic updates, REST API, XML-RPC, comments, and more.

Note: The free WPCode plugin has everything you need to add header and footer code in WordPress. However, if you want advanced features like a private cloud snippet library, conversion pixels, scheduled snippets, and more, you can upgrade to WPCode Pro.

For more details, read our guide on how to easily add custom code in WordPress.

With that said, let’s see how to easily add header and footer code in WordPress using the WPCode plugin.

Adding Code to Header and Footer in WordPress

The first thing you need to do is install and activate the free WPCode plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.

Once the plugin is activated, go to Code Snippets » Header & Footer from your WordPress admin panel. After that, you will see a ‘Header’ box where you can add your code.

Add code to header and footer with WPCode

If you scroll down, you’ll also see a ‘Body’ and a ‘Footer’ box.

Header and footer script boxes WPCode

Simply paste the code into one of the three boxes. Once you’re done, don’t forget to click the ‘Save Changes’ button to store your settings.

The plugin will now automatically load the code in the respective locations on your WordPress site.

You can always edit and remove any code that you don’t want to keep.

You’ll need to keep the plugin installed and activated at all times. Deactivating the plugin will stop adding all custom code to your site.

If you accidentally deactivate the plugin, the code will still be safely stored in your WordPress database. You can simply reinstall or reactivate the plugin, and the code will start appearing again.

Note: You may have to clear your WordPress cache after saving changes, so the code properly appears on the front-end of your website.

We have found the biggest reason why a lot of beginners use the WPCode plugin is to add Google Analytics to their website. For that, we actually recommend using the free MonsterInsights plugin.

MonsterInsights is the best Google Analytics plugin for WordPress. It helps you properly set up Google Analytics tracking with just a few clicks, and it shows you helpful stats right inside your WordPress dashboard.

For step-by-step instructions, see this tutorial on how to install Google Analytics in WordPress.

Video Tutorial

If you don’t enjoy following written instructions, you can watch our video tutorial on how to add header and footer code in WordPress:

Subscribe to WPBeginner

We hope this article helped you to learn how to add header and footer code in WordPress easily. You may also want to see our guide on how to display code on your WordPress site, or our expert picks of the best WordPress development tools.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Tag » Add Data To Head Section Wordpress