Importance and Need of child theme in WordPress

|
| By Webner

In wordpress, a child theme is a theme that inherits the functionality and styling of its parent theme.

Child themes are the recommended way of modifying an existing theme (like inheritance in object oriented programming). If we have any theme and we want to modify its header or any other files then on directly editing its header.php or any other file, we may lose the changes during updation (newer version) of the theme. Therefore we need to create child theme. Using child theme we can modify or edit any file without losing any functionality of actual theme.

How to create the child theme in WordPress:

We need to create a new folder for child theme inside “wp-content/themes“. It is recommended way that the name of your child theme directory is appended with ‘-child’. Naming it something like ‘Twenty Fifteen-child’, indicates that the parent theme is the child of Twenty Fifteen theme.

We don’t need to copy all the files of parent theme to child theme but only those files that we want to edit.

A child theme consists of at least two files (style.css and functions.php), which you will need to create:

style.css
functions.php

style.css: style.css must begin with code like this:

/*
 Theme Name:   Twenty Fifteen Child
 Theme URI:    http://example.com/twenty-fifteen-child/
 Description:  Twenty Fifteen Child Theme
 Author:       John Doe
 Author URI:   http://example.com
 Template:     twentyfifteen
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Tags:         light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
 Text Domain:  twenty-fifteen-child
*/

Here, We will replace the theme information like name,uri etc according to our theme.

If we need to include parent stylesheets in Child Themes, there is a wp_enqueue method for that:

<?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' );
}
?>

function.php:All custom functions which you want to add to your theme need to be written in the function.php file. If you want that child theme stylesheet should load after the parent css , then we need to add wp_enqueue_style() for child theme also.

When To Use a Child Theme:

<?php
function my_theme_enqueue_styles() {
$parent_style = 'parent-style'; 
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'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

If you have the plan to make only minor modifications to the parent them such as color changes or a different font then we don’t require it (we can simply override css).

However, if you need larger changes such as a complete design, multiple template changes etc then you should create a child theme instead of making changes in the parent theme so that all the changes will be retained even on parent theme updation.

These are the benefits of child theme:

1. Child theme makes code modifications portable and replicable.
2. Child theme keeps customization separate from parent theme functions.
3. Child theme allows parent themes to be updated without destroying your modifications.
4. Child theme allows you to take advantage of the effort and testing put into parent theme.
5. Child theme is a great way to start learning about theme development.

Leave a Reply

Your email address will not be published. Required fields are marked *