WordPress Child Themes

Categories: Resources and WordPress.

Before making any changes to a theme consider making a child theme and modifying that.
Your changes will not be lost when the theme is updated and your changes are easier to manage when they are not surrounded by the many lines of the main theme. I didn’t use a child theme here but I wish I did! Have a look at the official way here.

Adding the style.css file

Include the parent style.css file:

 
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
 
}
?>

Next add the child theme’s style.css file:

 
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
 
$parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
 
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
?>

Removing template files

Normally you don’t need to remove files from a template but sometimes the need arises. Simply deleting the file is not a good idea because the next time the theme gets updated the file will be restored and undo your work, which defeats the purpose of having a child theme.

For the sake of the exercise let’s assume we need to remove the home.php file from the theme. We could copy home.php to our child theme and delete most of the code from it but there is a better way.

We can tell WordPress to run the index.php file so that changes to index.php are run. The single line of code below is all that is needed to replace home.php with index.php. So in file called home.php in the child theme folder enter the following:

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

The locate_template function substitutes the index.php in the child theme, or if it’s not present the parent’s version of index.php is used. This way changes to index.php are respected and the home.php file is ignored.

(Ref: Sabine-Wilson, L. (2013). WordPress Web Design for Dummies. Hoboken, N.J.: John Wiley & Sons.)

Leave a Reply