Adding custom code or scripts to the header of a specific page

3d Animation

Adding custom code or scripts to the header of a specific page or post on a WordPress site.

The problem I had was I needed to add a Google conversion tag to the header part of the checkout page, and this page only on a WooCommerce site. I looked into several solutions and these all seemed to involve purchasing Premium WordPress plugins. Yes, these are easier solutions but I wanted to find something that didn’t cost me hundreds of pounds every year.

The solution was surprisingly simple!
1. Create a new template for the checkout page (or the page you want to add your unique code)

2. Create a new header.php file – eg. header-conversion.php

3. In the checkout page template – link to the new header file.

4. Add the Google script or conversion tag to the new header file.

5. Apply the new page template to the checkout page.

6. Job done!

Slightly more detailed instructions!
1. Create a copy of your header.php file – header-newpagename.php

2. Make necessary changes to the header-newpagename.php file

3. Create a new template page to load the new custom header.

4. To load the new header file change (see below)


<?php get_header(); ?> to <?php get_header('newpagename'); ?>