How to Create a WordPress Child Theme?

DateOct 4, 2018

WordPress Child Theme is extremely simple, so much so you can copy and paste my example below.

To create a child theme for your theme, you will need to do the following steps:

  1. Create a theme directory in your WordPress install
  2. Create a stylesheet with information about your child theme
  3. Pull in the styles of your parent theme

Once these steps are completed you can activate your child theme and your website will look exactly the same as before, but it will be using your child theme.

So let’s go through the above steps in detail. For this example, I will be creating a child theme for the Twenty Fourteen default theme.

 

1. First, go to your theme directory and create a folder for your new theme. You may name it anything you’d like. For clarity’s sake, I will name my theme twentyfourteen-child.

2. The next step is to create a stylesheet file. This must be named style.css. Copy and paste the following code into the file you’ve just created:

 

/*

Theme Name: Twenty Fourteen Child

Theme URI: http://yourwebsite.com/twentyfourteen-child/

Description: My first child theme, based on Twenty Fourteen

Author: Daniel Pataki

Author URI: http://danielpataki.com

Template: twentyfourteen

Version: 1.0.0

Tags: black, green, white, light, dark, two-columns, three-columns, left-sidebar, right-sidebar, fixed-layout, responsive-layout, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready, accessibility-ready, responsive-layout, infinite-scroll, post-slider, design, food, journal, magazine, news, photography, portfolio, clean, contemporary, dark, elegant, modern, professional, sophisticated

Text Domain: twenty-fourteen-child

*/

 

The two necessary items in the code above are the lines starting with “Theme Name” and “Template.” The theme name tells WordPress what the name of your theme is, and this is displayed in the theme selector. The template tells WordPress which theme it should consider as the parent theme. Most of the others are self-explanatory, with the exception of the text domain and the tags. The text domain is used for translating strings. The text domain should be unique for your theme and should be used whenever you use translation functions. See I18n for WordPress Developers for more information. The tags section is a list of tags that are used by the WordPress Theme Repository. For this example I looked at the style.css file of the parent theme and simply copy-pasted the tags from there.

In our case we do have a stylesheet, so WordPress figures it shouldn’t load the parent file’s. To make sure we load the parent file’s stylesheet we will need to enqueue it. This can be done in the theme’s functions.php file, so go ahead and create that file now. In this file, copy-paste the following code:

 

<?php
add_action( ‘wp_enqueue_scripts’, ‘enqueue_child_theme_styles’, PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( ‘parent-style’, get_template_directory_uri().’/style.css’ );
}
?>

 

If you have no idea about PHP and you just want to change some styles, don’t worry about why this works. Feel free to go into your stylesheet file now and start making your changes.

 

Child Theme Mechanics

So how does a child theme actually work? Child themes work on a file-level. When a file is used during the process of loading a theme it checks if it is present in the child theme. If it is, the content of that file is used. If it isn’t, the same file in the parent theme is used.

There is one exception to this rule, the theme’s functions file. The functions.php file in both the parent and the child theme is loaded. If the child theme’s functions replaced the parents you would either have a malfunctioning site, or you would need to copy-paste the entire contents of the parent theme’s function file into the child theme’s which would sort of defeat the purpose of extending a theme.

The workflow when modifying functionality is the following. If you want to make changes to the header, copy-paste the parent theme’s header.php file into your child theme. Edit the file to your heart’s content, save it and enjoy the fruits of your labour.

 

Some Notes For Theme Makers (WordPress Child Theme)

If you make your own themes there are a couple of guidelines you may want to follow to make child theme creation easier. The two most important ones are learning the difference between get_stylesheet_directory() and get_template_directory() and creating pluggable functions.

 

The Right Directory

When linking to assets using the mentioned functions you should always be aware that the get_template_ family of functions will always point to the directory of the parent theme while the get_stylesheet_ family of functions will always point to the child theme’s directory.

 

<a href=”http://twitter.com/danielpataki”><img src=”<?php echo get_template_directory_uri() ?>/images/twitter.png” alt=’Twitter Logo’>Follow Me</a>

<a href=”http://github.com/danielpataki”><img src=”<?php echo get_stylesheet_directory_uri() ?>/images/github.png” alt=’Github Logo’>On Github</a>

 

In the example above the first link takes its image from the parent theme, the second takes it from the child theme. There’s no good answer to which one you should use, it’s up to you.

The upside to using get_stylesheet_directory_uri() is that child theme developers can use their own image by simply creating it in the proper location. On the other hand, if the image doesn’t exist in the child theme it won’t be shown at all.

The reason for this is that if a child theme is active the get_stylesheet_directory_uri() function doesn’t check (and doesn’t know) which file you are loading so it won’t check for its existence, it will always spit back the URI for the child theme.

 

Modifiable Functions (WordPress Child Theme)

The other method you should use is what WordPress calls pluggable functions. This makes it possible for child theme authors to overwrite the functions you define in the parent theme. This involves wrapping your functions in function_exists() checks.

Let’s presume you create a function for a customized post meta display named my_meta(). There is no way a child theme can modify this function because it can not be defined twice. The solution is to only create this function if it hasn’t been defined (remember, the child theme’s function file is loaded first).

 

if ( !is_defined( ‘my_meta’ ) ) {

function my_meta() {

// code for postmeta here

}

}

 

Conclusion

Using a few very simple copy-pastable steps you can create a future-proofed environment for your theme, which will save you a lot of headaches. While it may be tempting to use the built-in theme editor in WordPress, it almost always causes more issues than it solves if you’re not using a child theme.

Take a few minutes to follow along the tutorial here and your website and your developer will thank you for it. Finally, If you have any great tips about child themes, do let us know.

Leave a Reply