Chapter 5

Understanding Parent and Child Themes

IN THIS CHAPTER

Check Defining the relationship between parent and child themes

Check Tweaking child themes with styles

Check Customizing child themes with images

Check Modifying child themes with template files

Using a theme exactly as a theme author released it is great. If a new version is released that fixes a browser compatibility issue or adds features offered by a new version of WordPress, a quick theme upgrade is very easy to do.

But there’s a good chance you’ll want to tinker with the design, add new features, or modify the theme structure. If you modify the theme, you won’t be able to upgrade to a newly released version without modifying the theme again. If only you could upgrade customized versions of themes with new features when they’re released! Fortunately, child themes give you this best-of-both-worlds theme solution.

This chapter explores what child themes are, how to create a child theme–ready parent theme, and how to get the most out of using child themes.

Customizing Theme Style with Child Themes

A WordPress theme consists of a collection of template files, stylesheets, images, and JavaScript files. The theme controls the layout and design that your visitors see on the site. When such a theme is properly set up as a parent theme, it allows a child theme, or a subset of instructions, to override its files, ensuring that a child theme can selectively modify the layout, styling, and functionality of the parent theme.

The quickest way to understand child themes is by example. In this section, you create a simple child theme that modifies the style of the parent theme.

Currently, the default WordPress theme is Twenty Nineteen. Figure 5-1 shows how the Twenty Nineteen theme appears on a sample site.

Screenshot depicting how the Twenty Nineteen theme appears on a sample site (WordPress All In One).

FIGURE 5-1: The Twenty Nineteen theme.

You likely have Twenty Nineteen on your WordPress site, and Twenty Nineteen is child theme–ready; therefore, it’s a great candidate for creating a child theme.

Creating a child theme

Like regular themes, a child theme needs to reside in a directory inside the /wp-content/themes directory. The first step in creating a child theme is adding the directory that will hold it. For this example, connect to your hosting account via Secure File Transfer Protocol (SFTP), and create a new directory called twentynineteen-child inside the /wp-content/themes directory.

To register the twentynineteen-child directory as a theme and to make it a child of the Twenty Nineteen theme, create a style.css file, and add the appropriate theme headers. To do this, type the following code in your favorite code or plain-text editor (such as Notepad for the PC or TextEdit for the Mac) and save the file as style.css:

/*
Theme Name: Twenty Nineteen Child
Description: My 2019 child theme
Author: Lisa Sabin-Wilson
Version: 1.0
Template: twentynineteen
*/

Typically, you can find the following headers in a WordPress theme:

  • Theme Name: The theme user sees this name in the back end of WordPress.
  • Description: This header provides the user any additional information about the theme. Currently, it appears only on the Manage Themes screen (which you access by clicking the Themes link on the Appearance menu).
  • Author: This header lists one or more theme authors. Currently, it's shown only on the Manage Themes screen (which you access by clicking the Themes link on the Appearance menu).
  • Version: The version number is useful for keeping track of outdated versions of the theme. It’s always a good idea to update the version number when modifying a theme.
  • Template: This header changes a theme into a child theme. The value of this header tells WordPress the directory name of the parent theme. Because your child theme uses Twenty Nineteen as the parent, your style.css needs to have a Template header with a value of twentynineteen (the directory name of the Twenty Nineteen theme).

Now activate the new Twenty Nineteen Child theme as your active theme. (For information on how to activate a theme on your site, check out Book 6, Chapter 2.) You should see a site layout similar to the one shown in Figure 5-2.

Screenshot displaying the WordPress For Dummies page depicting the Twenty Nineteen Child theme.

FIGURE 5-2: The Twenty Nineteen Child theme.

The theme shown in Figure 5-2 doesn't look quite right, does it? The problem is that the stylesheet (style.css) for the child theme is blank, and it’s overriding the stylesheet in the parent theme.

You could just copy and paste the contents of the parent theme’s style.css file into the child theme stylesheet, but that method would waste some of the potential of child themes.

Loading a parent theme's style

Remember One great thing about CSS is that rules can override one another. If you list the same rule twice in your CSS, the rule that comes last takes precedence.

Here’s an example:

a {
color: blue;
}

a {
color: red;
}

This example is overly simple, but it nicely shows what I’m talking about. The first rule says that all links (a tags) should be blue, whereas the second rule says that links should be red. With CSS, the last instruction takes precedence, so the links will be red.

Using this feature of CSS, you can inherit all the styling of the parent theme and selectively modify it by overriding the rules of the parent theme. But how can you load the parent theme’s style.css file so that it inherits the parent theme's styling?

Fortunately, CSS has another great feature that helps you do this with ease. Just add one line to the Twenty Nineteen Child theme’s style.css file:

/*
Theme Name: Twenty Nineteen Child
Description: My 2019 child theme
Author: Lisa Sabin-Wilson
Version: 1.0
Template: twentynineteen
*/
@import url('../twentynineteen/style.css');

Several things are going on here, so let me break the code down piece by piece:

  • @import: This piece of the code tells the browser to load another stylesheet and allows you to pull in the parent stylesheet quickly and easily.
  • url('…'): This piece of the code indicates that the value is a location, not a normal value.
  • ('../twentynineteen/style.css');: This piece of code is the location of the parent stylesheet. Notice the /twentynineteen directory name, which needs to be changed to match the Template value in the header so that the appropriate stylesheet is loaded.

Figure 5-3 shows how the site appears after the child theme's style.css file is updated to match the listing. It looks the same as Figure 5-1 except for the logo area in the header — an option that gets set in the Customizer and that hasn’t yet been set for this Twenty Nineteen child theme.

Screenshot depicting how the site appears after the child theme's file is updated to match the listing.

FIGURE 5-3: The updated child theme.

Customizing the parent theme’s styling

Your Twenty Nineteen child theme is set up to match the parent Twenty Nineteen theme. Now you can add new styling to the Twenty Nineteen Child theme’s style.css file. A simple example of how customizing works is to add a style that converts all h1, h2, and h3 headings to uppercase, like so:

/*
Theme Name: Twenty Nineteen Child
Description: My 2019 child theme
Author: Lisa Sabin-Wilson
Version: 1.0
Template: twentynineteen
*/

@import url('../twentynineteen/style.css');

h1, h2, h3 {
text-transform: uppercase;
}

Figure 5-4 shows how the child theme looks with the code additions applied. In the preceding code snippet, you targeted the H1, H2, and H3 heading tags for the site and inserted the value text-transform: uppercase, which made all of those heading tags appear in uppercase font (or all capital letters) Now the site title and post titles are now all uppercase, which differs from Figure 5-3.

Screenshot depicting how the updated child theme appears with all the heading tags set in uppercase font.

FIGURE 5-4: The updated child theme with uppercase headings.

As you can see, with just a few lines in a style.css file, you can create a new child theme that adds specific customizations to an existing theme. The change is quick and easy to make, and you don't have to modify anything in the parent theme to make it work.

Tip When upgrades to the parent theme are available, you can upgrade the parent to get the additional features, but you don’t have to make your modifications again because you made your modifications in the child theme, not the parent theme.

Customizations that are more complex work the same way. Simply add the new rules after the import rule that adds the parent stylesheet.

Using images in child theme designs

Many themes use images to add nice touches to the design. Typically, these images are added to a directory named images inside the theme.

Just as a parent theme may refer to images in its style.css file, your child themes can have their own images directory. The following sections show examples of how you can use these images.

Using a child theme image in a child theme stylesheet

Including a child theme image in a child theme stylesheet is common. To do so, you simply add the new image to the child theme's images directory and refer to it in the child theme’s style.css file. To get a feel for the mechanics of this process, follow these steps:

  1. Create an images directory inside the child theme's directory.

    A directory is also referred to as a folder. In your SFTP program, right-click inside the SFTP window, select New Folder from the shortcut menu, and name it images.

  2. Add to the directory an image to use.

    For this example, add an image called body-bg.png.

  3. Add the necessary styling to the child theme's style.css file, using a few of the properties covered in Book 6, Chapter 4:

    /*
    Theme Name: Twenty Nineteen Child
    Description: My 2019 child theme
    Author: Lisa Sabin-Wilson
    Version: 1.0
    Template: twentynineteen
    */

    @import url('../twentynineteen/style.css');

    body {
    background: url( 'images/body-bg.png' );
    background-attachment: fixed;
    background-position: top right;
    background-repeat: no-repeat;
    }

With a quick refresh of the site, you see that the site now has a new background. Figure 5-5 shows the WordPress logo displayed as the background image in the top-right corner. If you followed these steps, as you scroll down the site, you see that the image stays in place because you used the background-attachment: fixed; CSS rule, which fixes the background image in one spot.

Screenshot displaying the Twenty Nineteen child theme after the background image has been edited.

FIGURE 5-5: The Twenty Nineteen child theme after you edited the background image.

Using images in a child theme

Child theme images are acceptable for most purposes. You can add your own images to the child theme even if the image doesn't exist in the parent theme folder, and you can accomplish that task without changing the parent theme.

In the footer of the Twenty Nineteen theme, I added a WordPress logo to the right of the phrase Proudly powered by WordPress, as shown in Figure 5-6. By default, the logo doesn’t appear in the footer of the Twenty Nineteen theme.

Screenshot displaying the WordPress logo in the Twenty Nineteen child footer.

FIGURE 5-6: The WordPress logo in the Twenty Nineteen child footer.

Create a folder in your child theme called /images, add your selected images to that folder, and then call those images into your child theme by using the stylesheet (style.css) file in your child theme folder.

In this next example, add the WordPress logo to the right of the footer text. You can add a customization to the child theme's style.css file to make this change, as follows:

/*
Theme Name: Twenty Nineteen Child
Description: My 2019 child theme
Author: Lisa Sabin-Wilson
Version: 1.0
Template: twentynineteen
*/

@import url('../twentynineteen/style.css');

#colophon .site-info .imprint {
background: url( 'images/wordpress.png' );
background-position: top right;
background-repeat: no-repeat;
padding-right: 30px;
padding-top: 10px;
}

The CSS selectors (#colophon .site-info .imprint) in the code are from the Twenty Nineteen theme source code. (I wanted to target that footer text to add the logo to, so I looked at the HTML markup for the Twenty Nineteen parent theme to discover which selectors I needed to target.) Save the file, and refresh the site. Now you're showing WordPress pride.

Tip A zip file of the example Twenty Nineteen child theme, and associated code samples, found in this chapter can be downloaded here: https://lisasabin-wilson.com/wpfd/twentynineteen-child.zip.

Modifying Theme Structure with Child Themes

The preceding section shows how to use a child theme to modify the stylesheet of an existing theme. This technique is tremendously powerful. A talented CSS developer can use this technique to create an amazing variety of layouts and designs by using the features and functionality that exist in a parent theme. Creating a child theme removes, or at least reduces, the need to reinvent the wheel by re-creating all the templates in a theme.

This feature is just the beginning of the power of child themes, however. Although every child theme overrides the parent theme’s style.css file, the child theme can override the parent theme's template (PHP) files, too.

Child themes aren’t limited to overriding template files; when needed, child themes also can supply their own template files.

Template files are .php files that WordPress runs to render different views of your site. A site view is the type of content being looked at. Examples of views are home, category archive, individual post, and page content.

Examples of common template files are index.php, archive.php, single.php, page.php, attachment.php, and search.php. (You can read more about available template files, including how to use them, in Book 6, Chapter 3.)

You may wonder what purpose modifying template files of a parent theme serves. Although modifying the stylesheet of a parent theme can give you powerful control of the design, it can't add new content, modify the underlying site structure, or change how the theme functions. To get that level of control, you need to modify the template files.

Overriding parent template files

When the child theme and parent theme supply the same template file, WordPress uses the child theme file and ignores the parent theme file. This process of replacing the original parent template file is referred to as overriding.

Remember Although overriding each of the theme’s template files can defeat the purpose of using a child theme (updates of those template files won’t enhance the child theme), sometimes you must do so to produce a needed result.

The easiest way to customize a specific template file in a child theme is to copy the template file from the parent theme folder to the child theme folder. After copying the file, you can customize it, and the child theme reflects the changes.

A good example of a template file that can be overridden is the footer.php file. Customizing the footer allows for adding site-specific branding.

Adding new template files

A child theme can override existing parent template files, but it can supply template files that don't exist in the parent, too. Although you may never need your child themes to do this, this option can open possibilities for your designs.

In Book 4, Chapter 6, I cover different page templates you can add to your WordPress theme to achieve a different look and display different types of content on your site. In that chapter, you create an About Page template (about.php) that differs from the standard Page (page.php) template that already exists in the Twenty Nineteen theme. Applying the information, as you discover in Book 4, Chapter 6, you can create an about.php template; upload it to your Twenty Nineteen child theme; and then edit or adjust that file to suit your purposes.

The Twenty Nineteen parent theme doesn't have an about.php template, but because your child theme for Twenty Nineteen does, WordPress uses this template to display your About page on your website. WordPress is smart enough to recognize templates in a child theme, even if they don’t exist in the parent theme.

Removing template files

You may be asking why you’d want to remove a parent’s template file. That’s a good question. Unfortunately, the Twenty Nineteen theme doesn’t provide a good example, so you must use your imagination a bit.

Suppose that you’re creating a child theme from a parent theme called Example Parent. Example Parent is well designed, and you built a great child theme from it quickly. The child theme looks and works exactly the way you want it to, but there’s a problem: The Example Parent theme has a home.php template file that provides a highly customized nonblog home page. What you want, however, is a standard blog home page. If the home.php file didn't exist in Example Parent, everything would work perfectly.

You can’t remove the home.php file from Example Parent without modifying the theme, so you have to use a trick. Instead of removing the file, override the home.php file, and have it emulate index.php.

You may think that simply copying and pasting the Example Parent index.php code into the child theme's home.php file is a good approach. Although this technique works, there’s a better way: You can tell WordPress to run the index.php file so that the intended index.php file is respected. This single line of code inside the child theme's home.php is all you need to add to replace home.php with index.php:

<?php locate_template( array( 'index.php' ), true ); ?>

The locate_template function does a bit of magic. If the child theme supplies an index.php file, that file is used; if not, the parent index.php file is used.

This technique produces the same result as removing the parent theme's home.php file. WordPress ignores the home.php code and respects the changes in index.php.

Modifying the functions.php file

Like template files, child themes can provide a Theme Functions template, or functions.php file. Unlike in template files, the functions.php file of a child theme doesn't override the file of the parent theme.

When a parent theme and a child theme each have a functions.php file, both the parent and child functions.php files run. The child theme's functions.php file runs first; then the parent theme’s functions.php file runs. This setup is intentional because it allows the child theme to replace functions defined in the parent theme. Having two functions.php files works only if the functions are set up to allow having both, however.

The Twenty Nineteen functions.php file defines a function called twentynineteen_setup. This function handles the configuration of many theme options and activates some additional features. Child themes can replace this function to change the default configuration and features of the theme, too.

The following lines of code summarize how the functions.php file allows this to happen:

if ( ! function_exists( 'twentynineteen_setup' ) ) :
/**
* Sets up theme defaults and registers support for various
* WordPress features.
* Note that this function is hooked into the
* after_setup_theme hook, which
* runs before the init hook. The init hook is too late for
* some features, such
* as indicating support for post thumbnails.
*/

function twentynineteen_setup() {
endif;

Remember Wrapping the function declaration in the if statement protects the site from breaking in the event of a code conflict and allows a child theme to define its own version of the function.

In the Twenty Nineteen child theme, you can see how modifying this function affects the theme. Add a new twentynineteen_setup function that adds Post Thumbnails support to the Twenty Nineteen child theme's functions.php file.

<?php
function twentynineteen_setup() {
add_theme_support( 'post-thumbnails' );
}

As a result of this change, the child theme no longer supports other special WordPress features, such as custom editor styling, automatic feed link generation, internationalization and location, and any of the other features that the parent Twenty Nineteen theme supports.

The takeaway from this example is that a child theme can provide its own custom version of the function because the parent theme wraps the function declaration in an if block that checks for the function first.

Preparing a Parent Theme

WordPress makes it very easy for you to make parent themes. WordPress does most of the hard work, but you must follow some rules for a parent theme to function properly.

I've used the words stylesheet and template numerous times in this book in many contexts. Typically, stylesheet refers to a CSS file in a theme, and template refers to a template file in the theme. But these words also have specific meaning in the context of parent and child themes. You must understand the difference between a stylesheet and a template when working with parent and child themes.

Remember In WordPress, the active theme is the stylesheet, and the active theme’s parent is the template. If the theme doesn’t have a parent, the active theme is both the stylesheet and the template.

Technical stuff Originally, child themes could replace only the style.css file of a theme. The parent provided all the template files and functions.php code. Thus, the child theme provided style, and the parent theme provided the template files. The capabilities of child themes expanded in subsequent versions of WordPress, making the use of these terms for parent and child themes somewhat confusing.

Imagine two themes: parent and child. The following code is in the parent theme's header.php file and loads an additional stylesheet provided by the theme:

<link type="text/css" rel="stylesheet" media="all" href="<?php

bloginfo( 'stylesheet_directory' ) ?>/reset.css" />

The bloginfo function prints information about the site configuration or settings. This example uses the function to print the URL location of the stylesheet directory. The site is hosted at http://example.com, and the Parent is the active theme. It produces the following output:

<link type="text/css" rel="stylesheet" media="all"
href="http://example.com/wp-content/themes/Parent/reset.css" />

If the child theme is activated, the output would be

<link type="text/css" rel="stylesheet" media="all"
href="http://example.com/wp-content/themes/Child/reset.css" />

Now the location refers to the reset.css file in the Child theme. This code could work if every child theme copies the reset.css file of the Parent theme, but requiring child themes to add files to function isn't good design. The solution is simple, however. Instead of using the stylesheet_directory in the bloginfo call, use template_directory. The code looks like this:

<link type="text/css" rel="stylesheet" media="all" href="<?php
bloginfo( 'template_directory' ) ?>/reset.css" />

Now all child themes properly load the parent reset.css file.

When you're developing, use template_directory in stand-alone parent themes and stylesheet_directory in child themes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.239.46