Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 705
Alex Avatar asked Apr 26 '16 19:04

Alex


People also ask

How do I stop WordPress from updating my theme?

To turn off automatic updates for WordPress core, you just have to add the following code to your wp-config. php file: define( 'WP_AUTO_UPDATE_CORE', false );

Does updating WordPress affect my theme?

A WordPress update only changes WordPress files. It does not change plugin or theme files. Plugin updates affect only that plugin.

What is the difference between child theme and parent theme in WordPress?

➜ A parent theme is independent of any other theme for working while a child theme is dependent on the parent theme to work. ➜ A child theme can save you from losing your customizations even if you update the parent theme as all the customizations are stored in the child theme and you can view or edit them there.


2 Answers

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
};
like image 172
Nabeel Khan Avatar answered Sep 30 '22 07:09

Nabeel Khan


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.

like image 39
Tim Malone Avatar answered Sep 30 '22 07:09

Tim Malone