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?
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 );
A WordPress update only changes WordPress files. It does not change plugin or theme files. Plugin updates affect only that plugin.
➜ 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.
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
};
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 :)).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With