Customizing your WordPress web site utilizing code will be scary. Usually, you’ll want to dig into core recordsdata to modify configurations or add new parameters, which might break your web site when you go about it the mistaken method.
Nevertheless, including header and footer code tends to be a lot less complicated. Usually, you’ll solely want to copy and paste particular code snippets. This implies if you understand how to entry your header and footer recordsdata, you’re previous the scariest half.
On this article, we’ll train you the way to add header and footer code in WordPress each manually and with a plugin. Each approaches are very simple, so let’s get proper to it!
Why You May Want to Add Header and Footer Code in WordPress
Should you run an internet site, you’re in all probability accustomed to not less than one third-party service that requires you to embed code to join to it.
Among the hottest examples embody Google Analytics, Search Console, Fb Pixel, and many different instruments that contain web site monitoring.
All of these providers require you to add particular scripts between your web site’s head tags. Your web site’s header is an important part together with components akin to scripts, titles, model recordsdata, and extra.

Footer tags work equally to their head counterparts. Technically, when you add code snippets to your web site’s footer, they need to run the identical as when you embody them in your header.
Nevertheless, it’s vital to perceive that your web site will execute scripts in the order you embody them. Plus, the header code will run earlier than the web page is loaded, whereas the footer code will run after it.
You probably have the choice to select and you care about web site optimization, shifting JavaScript to your web site’s footer might help pace up loading instances. Nevertheless, some providers will explicitly ask you to add their code to your header to keep away from any execution points.
As a rule of thumb, if a service asks this of you, we advocate following its recommendation. For different JavaScript code snippets, place them into your WordPress web site’s footer. CSS code all the time goes into the header.
How to Add Header and Footer Code in WordPress (2 Strategies)
As is commonly the case with WordPress, you possibly can accomplish the identical purpose by both modifying recordsdata manually or utilizing a plugin.
Let’s begin by speaking in regards to the plugin strategy.
1. Use a Plugin Equivalent to Head, Footer, and Publish Injections
Utilizing a plugin to add code to your header and footer recordsdata in WordPress is the most secure strategy. With a plugin, you don’t want to modify theme recordsdata manually or be certain that you’re including scripts in the fitting place.
Whereas there are various good plugins to select from, our advice for the job is the Head, Footer and Publish Injections plugin.

This plugin offers you a number of management over the place in your code you need to add your code snippets. It additionally affords the choice of including completely different scripts for cellular and desktop renderings of your web site.
So as to add the plugin, go to Plugins > Add New inside your dashboard and use the search characteristic to discover the plugin. Click on on set up, then activate the plugin.
As soon as the plugin is lively, you possibly can leap straight to Settings > Header and Footer. You’ll see a number of fields the place you possibly can add code snippets.

Should you dig into the plugin’s choices, you’ll see that it affords a number of further placements on your code snippets. Nevertheless, in most circumstances, you possibly can stick to the Head and footer tab.
For world code, just remember to place these snippets throughout the On Each Web page subject. Nevertheless, when you solely want the code to run on your property web page, use the Solely On The Dwelling Web page choice.
When you end including code to your web site, save the modifications to the plugin and you’re good to go!
2. Add Code Snippets By way of Your features.php File
Including header and footer code manually in WordPress isn’t difficult, nevertheless it does require extra concerns than the plugin strategy.
You may add the code straight to your lively theme’s header.php or footer.php recordsdata. You’ll find these recordsdata throughout the /public_html/wp-content/themes/ listing by opening your lively theme’s folder and trying inside:

You may entry these recordsdata by way of File Switch Protocol (FTP) and edit them utilizing your favourite code editor. Nevertheless, modifying header and footer recordsdata straight will be messy. With this in thoughts, now we have two suggestions when you plan on utilizing the handbook strategy:
- Create a baby theme on your theme. This manner, you received’t lose any customizations once you replace the father or mother theme.
- Use hooks to add the code via your features.php file. This affords a cleaner strategy over modifying header and footer recordsdata manually.
Ideally, you must all the time use a baby theme when you plan on doing any kind of theme customization. You may entry your youngster theme’s features.php file in its folder throughout the /wp-content/themes listing.
Open the features.php file in your code editor. So as to add the code you need, you want to use both the wp_head or wp_footer hook.
The next code snippet provides your customized code to the header of your web site:
add_action('wp_head', 'test_script');
operate test_script() {
?>
<!-- Right here comes your customized code in HTML format. -->
<?php
}
You need to use the wp_footer hook as an alternative of wp_head if you would like to place the code into your footer. You can even change the operate’s identify (test_script) if you would like.
While you’re carried out tweaking your features.php file, bear in mind to save your modifications, and that’s it. No matter code you added ought to be working now!
Conclusion
Many third-party providers akin to Google Analytics or Fb Pixel require you to add code snippets to your web site in order that they will join to it. Nevertheless, this is only one of many circumstances the place you’ll want to add code to your header and footer in WordPress, so it pays to understand how to do it.
There are two methods you possibly can add code snippets to your header and footer. You may both use a plugin akin to ‘Head, Footer and Publish Injections’, or add code snippets via your features.php file.
Do you have got any questions on how to add header and footer code in WordPress? Let’s discuss them in the feedback part beneath!