I have a problem with child themes and I’m surprised I don’t see it discussed more. Maybe it’s just so unlikely it’s not worth worrying about. Or maybe not…
Why Use a Child Theme?
There are lots of good reasons for using a child theme but the main one is that it allows you to modify the theme files (either the source files themselves, the PHP code, or the CSS styling of the theme) in such a way that those changes are preserved even after you update your parent theme to a new version. If the changes were made to your theme files they would overwrite during a theme update. Using a theme, in effect, creates a parallel and separate set of files that are not overwritten by a theme update. Sounds good, and it is good 🙂
Making Changes to a Theme Without a Child Theme
Just for a moment imagine there was no such thing as child themes. In that scenario, we would be forced to make changes to the theme files themselves. In order to make an update to the theme, we would first need to identify all of our changes and apply them to the new version of the theme. A time consuming job that is easy to get wrong. Most people have better things to do with their time 🙂
OK, So What is the Problem With Child Themes?
Child themes are specific to their parent, because they contain the source code of the parent. If you went to the trouble of moving the child theme files to a different parent, the chances of a good outcome would be small. No one would expect this to go smoothly.
The thing is, whenever we update our parent theme without changing the child files, we are, in effect, ‘reparenting’ the child. Whenever you update the theme you are creating a hybrid theme, made up of files from the new version of the parent and files from the earlier version of the parent that you used to make the child. Now, this isn’t going to be an issue if all you have done in the child is modified the CSS style file. Neither is it likely to be much of an issue if all you have done is made some changes to the functions.php file. However, if you have copied other PHP files from the parent theme and used them in your child, those PHP files come from the version you happened to be using when you did this and won;t get updated when you update the parent. Ironically, you really want the source code in the child theme to be updated, but for your changes to survive.
Here’s an example. If I am using V1.0 of a theme and I create a child theme using some of the PHP files from it then update my theme to V2.0 the files I copied into the child and modified came from V1.0 and it is those V1.0 files that are going to be used by the V2.0 theme even after it has been updated.
What Might Happen?
Well, if your theme is a mixture of code from two different versions of the same theme….. I’m not sure what might happen 🙂 But it just might cause some rather strange and subtle issues with your website. Or it might just not work at all. The most likely outcome though is that it will be fine. My feeling is that the sort of files that I want to modify in my child theme are the sort of files that aren’t going to be changes much by the theme developers. You would imagine that the theme developers are careful about this sort of thing and would try to minimise the chances of problems.
How to Update a Theme That Has a Child
Ok, this is the easy part. The way to avoid any problems when updating a theme that has a child is clearly….. to identify all of the changes you have made to the PHP files in the child, and apply those changes again to the new versions of the source files from the updated theme. Then use those the modified version of the updated theme in your child. In effect, re-creating the child based on the updated parent. Which sounds a lot like the same amount of work involved in the scenario described above where you didn’t have a child theme but wanted to make changes…..
So when making changes in your child you really need to be able to identify them. Three golden rules here that I can think of at the moment 🙂
First, comment out code rather than deleting it. This leaves the existing code where it was so you do not destroy the original structure. If you just delete code, you can;t put it back easily and you could make it a lot harder to compare your modified files with the originals (because the structure is lost).
/* CM 01-12-16 Take this out CR0689 */ /* some-code(); more-code(); */
Second, if you are going to modify code, then modify a copy rather than the original. Just comment out the original. Again, this makes it easy to see you your change relates to the original code.
/* CM 01-12-16 Change this CR0690 >>> */ /* $x = something(); */ $x = something-else(); /* CM 01-12-16 Change this CR0690 <<< */
Third, use lost of comments, especially to help in identifying the scope of your changes. So in the above I have a comment at the start and at the end of the scope of the change. I have also documented the changes elsewhere in more detail and provided a reference in the code – CR0690. Noting your changes somewhere else is useful because you can provide more detail, manage, review and organise the changes (perhaps for clients) and also, in situations where multiple changes are made in order to make a change, all of the changes can be found by simply searching for, in this case, CR0690.
Is it Worth Using a Child Theme?
Damn right it is. A child theme is a fantastic way of building on an existing theme, of using the parent to give you a flying start in your website build. It also makes it much easier to partition off and identify your changes. It’s a great way to make both global and individual styling changes using the child CSS style file. It allows you to create custom templates.
A Couple of Child Theme Resources
A fantastic article – How To Create And Customize A WordPress Child Theme
For those who prefer videos – How to Create a WordPress Child Theme