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.
andrew says
Hey Brad, When i try to add the custom function to my functions.php file, special characters in my javascript conversion snippet cause php errors and close it out before (‘).
Do you have any way how to work around this?
Brad Dalton says
Hello Andrew
Use the scripts box if your theme includes one and wrap your script in scripts tags.
Otherwise, you can use this method http://wpsites.net/tools/how-to-install-buysell-ads-bsa-code-manually-in-any-theme/
Christian Karasiewicz says
Hi. I am running Genesis, but do not see the custom tracking/conversion code box. Do I need to turn anything additional on to get this to display on a post or page?
Brad Dalton says
Go to Genesis > Theme Settings > Header and Footer Scripts and paste it in there.
Christian Karasiewicz says
I’m adding a script that needs to go on a specific post or page, not site-wide.
Brad Dalton says
The script box for single posts and pages is only available for Genesis 2.0.
If you’re running Genesis 2.0, you’ll see it below your editor on all edit screens.
Otherwise, go to the top right hand corner and click screen options where you can check the box to show the field.
Christian Karasiewicz says
Strange…I’m running WP 3.6.1 and Genesis 2.0 and don’t have it.
Brad Dalton says
I’m running 3.7 so maybe that’s why i have it.
You can load scripts conditionally on one page using code.
What is the script?
Christian Karasiewicz says
Did it get renamed by chance? Is it now the Scripts box?
Christian Karasiewicz says
Got it figured out. It was renamed to Scripts. Thanks for the help!
Bradley Charbonneau says
Thanks for the excellent post, Brad! I love WOO Themes but I think their hook manager doesn’t allow PHP so I can’t do the condition PHP for a certain page. I’m currently using this plugin which lets me put code on certain pages, but ugh, yet another plugin. Thoughts?
Brad Dalton says
Hi Bradley. I’ll look for a plugin which works with all themes and let you know. I know adding code directly to the template files isn’t desirable so i need to find a better solution for non Genesis/Thesis users.
Brad Dalton says
I’ve written a custom function for you which i tested on Woo’s Canvas theme. You’ll need to replace the page I.D and add your tracking code before adding this to your child theme.