Many different themes provide a custom field for adding tracking code to single posts and pages in WordPress.
Here’s an example of what Genesis offers:
And here’s an example of what Thesis offers:
If you’re using a free theme like Twenty Eleven or Twenty Twelve, i’ve got a solution for you abit further on in this post.
Theme With NO Tracking Code Fields
Both the premium theme frameworks above include this field as shown but Not if you install an SEO plugin. When you do this, the default theme SEO fields for adding scripts and tracking code to individual pages and posts deactivates.
So whats the fix?
Genesis
With Genesis, you can install the Simple Hooks plugin and add some php code with a conditional tag and your tracking code/script to the WordPress head hook.
Here’s the sample code you can paste in the field, change the page I.D to the one you want to track and add your tracking code between the html tags.
<?php if (is_page(200) ) { ?><div class="tracking-code">Insert-Code-Here</div><?php } ?>
Thesis
With Thesis, you can install the Thesis Hook plugin and do pretty much the same thing as what you can do with Genesis.
WOO
Woo themes also offer a hook manager built into some of their themes. The hook manager isn’t as flexible as the Genesis and Thesis hook plugins and only executes shortcodes and HTML.
You’ll need to write a custom function or add the code to the header.php if using a Woo theme.
Custom Function Works With All Themes
Here’s the code which works with ALL themes:
add_action( 'wp_head', 'wpsites_add_tracking_code' );
function wpsites_add_tracking_code() {
if ( is_page(200) ) {
echo'<div class="tracking-code">add your tracking code here.</div>';
}
}
You’ll need to change the page I.D and add your tracking code before pasting this code into your child themes functions.php file. You can also use the: You can add custom functions below section in the Woo themes parent function.php file.
WordPress Hooks
wp_head Executed before the closing head tag
wp_footer Executed before the closing body tag
Change the WordPress hook if you want your tracking code to execute in your footer.
Adding Code To Header.php
If your theme doesn’t offer an easy way to hook in code on specific pages using conditional tags, you can still get the job done fairly easily.
You’ll need to edit your header.php file and add the code i used above for Genesis before the closing head tag.
This method will work with any theme.
Here’s a screenshot of what i’ve tested on the Twenty Twelve default theme for WordPress.
Note the php code with conditional tag and page i.d highlighted in yellow.
The Twenty Eleven theme and many other free themes will require you to add the code to the header.php file.








Leave a Reply