WordPress child theme is useful because it provides a safe and secure way to make changes to a website’s design and functionality without affecting the parent theme. This allows you to upgrade your parent theme without breaking anything on your website. In this article, I will walk you through the steps of how to create a child theme in WordPress.
A WordPress child theme inherits all the functionalities and styles of the parent theme. It also helps to maintain the stability of the site, as any changes made to the child theme will not affect the parent theme’s code, ensuring that the site is functional.
So it is safe to say that you won’t lose any functionality of the parent theme.
Initialization
Let’s say we have a theme called paper in our wp-content/themes/
directory. What we will do is create a new directory and name it paper-child. -child
is required to be appended to the parent theme name.
Structure
WordPress child theme has two parts:
- Stylesheet
- functions.php file
Create a style.css
file and paste or type the following in it:
/**
* Theme Name: Paper Child
* Description: Paper Child Theme
* Author: M M Arif
* Template: paper
* Text Domain: paper
* Version: 1.0.0
*/
The contents of the style.css are self explanatory but note that Template is your parent theme name. The rest you can change to your will. You can now add any custom CSS styles, whether to override the styles of the parent theme or to introduce new styles.
Next create a functions.php file and add the following to it.
<?php
if (!defined('WP_DEBUG')) {
die( 'Direct access forbidden.' );
}
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
});
// Add your custom functions from here
We are extending our parent CSS style here and blocking direct access to this file.
Screenshot
It will be a nice addition to add a screenshot same as the parent image dimension (most are 1200×900) and make it look different from the parent theme. Filename should be screenshot.jpg.
At the end you will have three basic files in the child theme directory:
- style.css
- functions.php
- screenshot.jpg
From here on you can upload the theme and enable it in WordPress themes section.
Note: this is not the limit you can enhance and add anything you would like to the child theme.