Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Add Code to the Header and Footer of WordPress

Insert tracking codes, styles and other scripts into every page of your WordPress site.

WordPress2 min read9 steps3 screenshots
  • Add analytics, such as Google Analytics.
  • Add custom CSS.
  • Set a favicon.
  • Verify your site with search engines.
  • Add scripts for ads or social media.

There are four ways to do it. The plugin is the easiest and safest.

Method 1: Use a plugin

  1. In the dashboard, go to Plugins, then Add New.
  2. Search for Insert Headers and Footers. Click Install Now, then Activate.
    The Insert Headers and Footers plugin
  3. Go to Settings, then Insert Headers and Footers.
  4. Paste your code in the Header or Footer box.
    The header and footer code boxes
  5. Click Save.

Method 2: Edit functions.php

Go to Appearance, then Theme File Editor. Open functions.php.

The functions.php file in the theme editor

Add this code for the header:

function add_custom_code_to_header() {
    echo '<!-- Your custom header code here -->';
}
add_action('wp_head', 'add_custom_code_to_header');

Add this code for the footer:

function add_custom_code_to_footer() {
    echo '<!-- Your custom footer code here -->';
}
add_action('wp_footer', 'add_custom_code_to_footer');

Click Update File.

Open Appearance, then Theme File Editor, and choose header.php or footer.php. Put your code before <?php wp_head(); ?> in the header, or before <?php wp_footer(); ?> in the footer. Save the file.

Method 4: Use a child theme

An update of your theme can wipe your edits. A child theme keeps them safe.

  1. Create a folder in wp-content/themes, for example my-child-theme.
  2. Add a style.css file to the folder:
/*
 Theme Name:   My Child Theme
 Template:     parent-theme-folder
*/
  1. Add a functions.php file to the folder:
<?php
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
  1. Activate the child theme under Appearance, then Themes. Now use Method 2 or 3 inside the child theme.
Warning: Back up your site before you edit theme files. A small typo in functions.php can make the site show a white screen.

Quick recap

  • A plugin is the safest way.
  • Theme files work too, but updates can remove your changes.
  • Use a child theme for lasting edits.
  • Only add scripts from sources you trust.