Struggling with how to add code to the WordPress header (head) or footer? Many tools and tracking scripts crave you to add lawmaking snippets to your WordPress site's header or footer. But, by default, WordPress doesn't get in very attainable to edit those files.

In this entry, we'll evidence you how to add code to the WordPress header or footer both past using a WordPress plugin or manually adding the code to your functions.php file. For most users, the plugin method is the recommended method. Merely intermediate-avant-garde WordPress users may prefer using the manual lawmaking method, instead.

You can click the links beneath to spring directly to your preferred method:

  • How to Add Code to WordPress Header and Footer With a Plugin
  • How to Manually Add Lawmaking to WordPress Header and Footer

How to Add together Code to WordPress Header and Footer With a Plugin

For nigh users, the easiest manner to add code to the WordPress header and footer is via a plugin. The benefits of using a plugin over the manual method in the next section are:

  • Your code snippets will remain intact if y'all ever switch themes.
  • The plugin makes it easy to only add code snippets to the header or footer of your homepage, which tin be helpful in some utilize cases.
  • The plugin is more than beginner-friendly considering it doesn't require digging into the lawmaking.

While there are several plugins that offer this functionality, nosotros recommend the Head, Footer and Post Injections plugin from Stefano Lissa considering it gives you more than control than many of the alternatives. It currently has over 200,000 agile installs with a v out of v-star rating.

Header and Footer WordPress plugin
Header and Footer WordPress plugin

Pace 1: Install and Activate Head, Footer and Post Injections

Head, Footer and Post Injections is available for free at WordPress.org, and so you can install and activate it straight from your WordPress dashboard by going to Plugins → Add together New and searching for it:

Install the plugin
Install the plugin

Step ii: Add Code Snippet to Head, Footer and Postal service Injections

In one case you activate the plugin, you tin can admission its interface past going to Settings → Header and Footer in your WordPress dashboard. You'll see a number of tabs in the plugin'due south interface. But for this guide, you'll by and large work in the default Head and footer tab:

Head, Footer and Post Injections interface
Head, Footer and Mail Injections interface

To add code to your site's header, y'all demand to paste information technology into i of the boxes under the <HEAD> Section INJECTION expanse:

  • Every folio – adds the code snippet to the header of every unmarried page on your site. This is what yous'll want to apply most of the time.
  • Merely home page – merely adds the code snippet to the header of your homepage.

For instance, to add the Google Analytics tracking code to WordPress, yous'd just paste it into the Every page box and save your changes:

Subscribe Now

Example of adding code to header
Instance of adding code to header

To add code to your site'due south footer, you tin whorl down to the BEFORE THE </BODY> CLOSING TAG (FOOTER) option. Again, you accept 2 options, though they're different from the header section:

  • Desktop – despite the name, this adds code to the footer of both the desktop and mobile version of your site unless you specifically bank check the box for Mobile.
  • Mobilewhen checked, this lets you lot add together a different code snippet to the mobile version of your site.

If you but want to add a code snippet to the footer department for all users, no matter their devices, paste information technology in the Desktop box and leave Mobile unchecked:

Example of adding code to footer
Example of adding code to footer

If needed, you tin can add multiple code snippets to each box.

BONUS – Add together Lawmaking to Header and Footer of Google AMP Pages

Some other neat matter about this plugin is that, if you're using Google AMP for WordPress, the plugin lets you specifically add together code snippets to the header and footer of the Google AMP versions of your pages (every bit long as you're using the official AMP plugin from Automattic).

To practice it, caput to the AMP tab in Header and Footer and paste your code snippet into the appropriate box:

Google AMP interface
Google AMP interface

If you lot are only needing to add together different PHP functions on a site-broad basis, we likewise recommend checking out the free Code Snippets plugin. It removes the demand to add together custom snippets to your theme's functions.php file. It currently has over 100,000 active installs with a five out of 5-star rating.

Code Snippets WordPress plugin
Code Snippets WordPress plugin

How to Manually Add together Code to WordPress Header and Footer

If you lot're non familiar with the basics of PHP, we recommend yous stick with the plugin method to a higher place. The manual method might be overly complicated for you.

In this section, yous'll learn how to manually add code snippets to your theme's header and footer via its functions.php file.

If you lot want to proceed with this method over the plugin in the previous section, information technology's essential that you utilize a WordPress child theme to make your edits. If you don't use a kid theme, whatever code that yous add to your header or footer will get overwritten if y'all update your WordPress theme.

Many developers provide a child theme. But if your programmer doesn't, here's a guide on how to create a WordPress child theme. One time y'all have your child theme gear up to go, y'all can proceed with the following steps to add lawmaking to your theme's header or footer.

While you can add code snippets directly to your header.php and footer.php files, a meliorate way is to utilize your functions.php file and the appropriate WordPress hook. This lets you lot go on all your snippets in one place and avert modifying core theme files.

Step 1: Gear up Code Snippets

To get you started, we'll give you a rough framework to add code to both your header and footer.

To add code to your header, employ this code snippet:

          /* Describe what the code snippet does so yous can remember later on */ add_action('wp_head', 'your_function_name'); part your_function_name(){ ?> PASTE HEADER Lawmaking Here <?php };                  

To add code to your footer, use this code snippet:

          /* Describe what the code snippet does so you can call back later on */ add_action('wp_footer', 'your_function_name'); office your_function_name(){ ?> PASTE FOOTER Lawmaking HERE <?php };        

For each snippet, make sure to alter:

  • The comment description (helpful when you need to call back what a lawmaking snippet does a year subsequently)
  • The your_function_name placeholder (both instances)
  • The PASTE Ten Lawmaking HERE placeholder

Step ii: Add together Code Snippets to functions.php File in Child Theme

One time you have the relevant code snippet(s) ready, you need to add them to the functions.php file of your child theme. Y'all can either edit this file by connecting to your site via FTP. Or, you can go to Appearance → Editor and select the functions.php file. Then, paste your code at the end of the file:

Add code to functions.php file
Add together code to functions.php file

Brand sure to relieve your changes and you're done!

BONUS: Add Code to Header or Footer For Only Specific Pages

If you lot want more control over where your header or footer lawmaking snippets prove upward, you can use if statements to just add the code to specific pages on your WordPress site.

For instance, to just add code snippets to the header or footer of your homepage, y'all could utilize:

          /* Describe what the lawmaking snippet does then y'all tin can recollect after on */ add_action('wp_head', 'your_function_name'); function your_function_name(){ if(is_front_page()) {  ?> PASTE HEADER Lawmaking HERE <?php  } };                  

Another selection is to just add the code snippets to specific posts or pages. To practise that, you tin can use this code snippet:

          /* Describe what the code snippet does so yous can think afterward */ add_action('wp_head', 'your_function_name'); function your_function_name(){ if(is_single(73790)) {  ?> PASTE HEADER CODE Hither <?php  } };                  

Make certain to replace the example number – 73790 – with the actual ID of the post or folio you want to add the lawmaking snippets to.

When In Doubt, Use The Plugin

That wraps up our guide on how to add together lawmaking to the header or footer of your WordPress site. If the manual lawmaking examples are confusing, nosotros recommend that yous use the plugin method. It'southward much more than beginner-friendly and, most of the time, gives you simply as much functionality.


Save fourth dimension, costs and maximize site performance with:

  • Instant assist from WordPress hosting experts, 24/vii.
  • Cloudflare Enterprise integration.
  • Global audition reach with 29 data centers worldwide.
  • Optimization with our built-in Application Performance Monitoring.

All of that and much more, in ane plan with no long-term contracts, assisted migrations, and a 30-24-hour interval-money-back-guarantee. Check out our plans or talk to sales to find the programme that's right for y'all.