I have currently manually implemented a tracking code in wp-content/themes/genesis/header.php
The code looks like this (shortened):
<script>
CODE HERE
<?php if (is_single()){CODE HERE}?>
CODE HERE
</script>
</head>
Whenever I upgrade genesis (the WordPress theme) this code is lost and I have to manually add it again.
How can I add this code via the functions.php
to the head section in wp-content/themes/genesis/header.php
so that it survives a WordPress theme upgrade – how would the code look?
You need to use
wp_head
hook to add content to the<head></head>
dynamically.Your code would look like this:
Generally, the solution for modifying your theme without having your modifications overwritten is using a child theme. But you could also create a small plugin that would do the same thing you want to do here.
Which option you take is generally much of a muchness for now, but if you are planning more changes in the future, you should keep in mind that:
This might help you decide which option is best to take now (although you can easily do both, or change later if you wish :)).
Option 1: Creating a child theme
Create a new folder in the
wp-content/themes
folder (name it whatever you’d like to call your new theme), and then create astyle.css
in that folder.At the top of
style.css
you’ll need to include defining information for your theme. You can copy the format for this from the Genesis theme, just change the name and other details so it’s clear when you go to activate it that this is your theme.The key here is then to add a new line to this theme info reading:
Template: genesis
That line tells WordPress that your theme will be a child theme of Genesis, and anything your theme doesn’t provide, WordPress will grab from Genesis.
The key here is then to override only what you want to and let the rest fallback to Genesis.
So, you could copy the
header.php
and add your code in, but then you’ll still need to update the rest of the file if it changes. A better solution would be to create your ownfunctions.php
in your new child theme and use the following:This will then hook into WordPress’ head action and print out the tracking code right where you want it, without you having to muck around with the rest of the header.
Of course, once you’re ready, go to Appearance -> Themes in WordPress and you’ll see your new theme there. Activate it and check your site!
For more background and tips on child themes you can see this page on the WordPress Codex.
Option 2: Creating a plugin
If it’s just functionality you want to add to your site, you may find a plugin more helpful – particularly because you can change themes later and easily keep your plugin, and you can activate it and deactivate it at will.
You can create as many plugins as you like if there is more functionality you want to add later.
The process is fairly similar to creating a theme above. Instead of creating the new folder in the
wp-content/themes
folder, stick it inwp-content/plugins
instead. Then, create a.php
file in that folder (eg.myplugin.php
, but you can call it whatever you like), and add the following to the top of the file:(You can add additional information if you wish, more information is available on this page of the WordPress Plugin Handbook)
Under this, simply place the exact same
add_action()
code mentioned in the theme option above.Save your file, go to Plugins in your WordPress admin, find your new plugin in the list, click Activate, and check your site!
For more background and tips on plugins you can see this page on the WordPress Codex.