Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

120 PART II • Designing and Developing WordPress Themes


THE BRILLIANCE OF CHILD THEMES


Child themes let you create themes that rely on other themes, called parent themes, as templates,
by changing parts of the parent theme to suit your own needs. For example, say you love a
particular theme but dislike the fonts and colors. You may also think that it needs a few Page
templates to meet your needs. There are two ways to tackle this problem. The most obvious
method is the direct route: Just open the theme’s files and edit them to your heart’s content. In
this case, that would mean making some changes in style.css (for the fonts and colors) and
adding a couple of Page templates. No big deal, right?

Wrong. What happens when the theme author updates the theme with brand-new functional-
ity, and you, giddy with joy, upload the new version — and see all your edits go away?
Obviously, your edits, with the colors you changed and your Page templates, aren’t included in
the original author’s theme, so now you’ll have to re-create all your adaptations so that the
theme fits your needs again. You can of course keep notes on what you change and back up
your altered files, but the new version of the theme may have several changes and (re)applying
your edits will be at best a nuisance — and at worst, tricky and time-consuming.

Editing a theme may be a simple solution, but if you want to be able to upgrade it with new
versions with your edits intact, there is a better way. You can create a child theme, using the
original theme as the parent theme (or template, as it is called when defining it). The child
theme sits in its own folder, and so do all its associated files, so when you upload the new
version of the original theme that you’ve built your site on, you’ll overwrite only that theme’s
files, and not your child theme, which contains all your changes. In other words, none of your
edits will go away when updating the main theme. The whole idea is to separate the main
theme functionality, code, and content from your own edits and adaptations. And because
those will reside in your child theme’s area, they are safe from the parent theme’s updates.
Figure 5-1 illustrates the child theme concept.

HOW CHILD THEMES WORK
Any theme can be the parent of a child theme. The parent theme must be located in your
wp-content/themes/ folder (because otherwise you can’t use its files) and the child theme in
its own folder, just like a regular theme. For example, let’s use the Notes Blog theme as a
parent theme. This is a simple blog-like theme created for this very purpose, so it will suit the
needs for this example. You can download it from http://tdh.me/wordpress/notes.
Make sure that the Notes Blog folder, called notes-blog, is in the wp-content/themes/ folder.
Then add your very own Small Notes child theme (or whatever you want to call it) in its own
folder, also within wp-content/themes/.

After that, you need a style.css file to tell WordPress that it is a theme, and in fact a child
theme, and point to the parent theme. Whenever a template file is called for, WordPress will
look for it within the child theme, and if it isn’t there, it’ll load up the one in the original
parent theme.
Free download pdf