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

17,103

Solution 1

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
};

Solution 2

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.

Share:
17,103
Alex
Author by

Alex

Updated on July 23, 2022

Comments

  • Alex
    Alex almost 2 years

    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?

  • Tim Malone
    Tim Malone about 8 years
    If placed in the theme's functions.php, this would still be overridden when the theme is updated. However, placed in a plugin or a child theme would be fine.
  • Nabeel Khan
    Nabeel Khan about 8 years
    yap, he can place it anywhere he wants
  • jsonV
    jsonV over 4 years
    IMO, this question is more about how to avoid rewriting functions.php every update... thus Tim's answer is more appropriate.