For example, maybe you only want to add a certain code snippet to your homepage but not your blog posts. Or, maybe you only want to execute a certain snippet for anonymous visitors, but not logged-in visitors.
Here are some examples of when these plugins work well:
- Adding tracking scripts: Google Analytics, Facebook Pixel, Pinterest Pixel, etc.
WPCode (Formerly Insert Header and Footers)
The plugin includes some preset snippets that you can use. However, to add your own custom snippet, just select the option for Add Your Custom Code (New Snippet) by hovering over it and clicking Use Snippet.
Below, you can see what it looks like to add the Google Analytics tracking code.
Note: WPCode will automatically add the opening and closing <script> tags, so you don't need to include them in the code.
If you want even more control over when to use this snippet, you can add more conditional logic using the Smart Conditional Logic feature. For example, this feature would let you only load the code for certain types of users.
For most situations, though, you only need to use the Insertion settings:
When you're happy with the settings, click the button in the top-right corner to Save Snippet. Then, change the toggle beside it to Active to make the snippet live on your site:
In general, it has a similar feature list to the WPCode plugin above. But, one small advantage is that its free version lets you target snippets to specific user devices (e.g. mobile vs desktop visitors).
However, the free version of the plugin lacks the other user-specific targeting rules of WPCode, so there are pros and cons. The Pro version of Header Footer Code Manager does add support for user targeting, among other features.
At the top of the interface, you can configure the settings for this snippet:
- Name: An internal name to help you remember what this snippet does.
- Site Display: Choose where to add this snippet. You can add it sitewide or to specific types of content. Or, you can manually place it using a shortcode by choosing the Shortcode Only option.
- Exclude Pages / Posts: You can manually exclude individual pieces of content. For example, you could add the snippet sitewide except for your blog listing page.
- Location: Choose the location in your site's code to add the snippet. You can add it in the header or footer.
- Device Display: This gives you the option to only load the snippet for users with a certain type of device (e.g., users who are browsing on a mobile phone).
- Status: Set this to Active to enable the snippet on your site.
- wp_head: Adds content to the header of the page. In code terms, it adds code inside the <head> section.
- wp_footer: Adds content to the footer of the page.
However, we'll provide plenty of code examples to work from, so you should be able to follow this guide even if you don't have much PHP experience.
To add these code snippets to your site, you have three different options:
- functions.php file of your theme: If you add code to your theme's functions.php file, make sure to use a WordPress child theme so that your code snippets don't get overwritten when you update the main theme.
- Custom plugin: You can create a custom plugin to house your code snippets. It's a little more complex to set up but it eliminates the need to use a child theme.
Here's an actual example of what this might look like if you're using this approach to add Google Analytics to WordPress using the older Universal Analytics method:
To find the ID of a post, open the content editor for that post and look at the URL. You should see post=[number] at the end of the URL. That number is the ID of the post.
However, you'll need to switch the conditional from is_single to is_page like so:
You can follow the same instructions from the previous section to find the ID of the page that you want to target.
Enqueuing the script accomplishes two things:
- It registers the script with WordPress.
- It tells WordPress where/when to load that asset.
If you're interested in this approach, you can follow our complete guide on how to enqueue scripts in WordPress.
- Manually inject the code snippets using PHP and the WordPress hook system.