How can I avoid Wordpress theme upgrades changing my parent themes functions.php?

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:

  • plugins are for adding functionality
  • themes are for controlling how things look and feel

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 a style.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 own functions.php in your new child theme and use the following:

add_action('wp_head', function(){
  ?>
  Enter tracking code here...
  <?php
});

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 in wp-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:

<?php
/*
Plugin Name: My Toolset
*/

(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.


You need to use wp_head hook to add content to the <head></head> dynamically.

Your code would look like this:

add_action('wp_head', 'change_this_name');
function change_this_name(){
  ?>
  <script>
    CODE HERE
  <?php if (is_single()){CODE HERE}?>
    CODE HERE
  </script>
  <?php
};