WordPress is the world’s most popular content management system (CMS), powering over 40% of all websites on the internet. One of the reasons for its popularity is its flexibility and ease of customization, which is made possible by the use of themes. Themes in WordPress control the visual appearance of a website and can be customized in many ways. However, modifying a theme directly can have its drawbacks, including the loss of customizations during theme updates. This is done child theme in WordPress.
Fortunately, WordPress provides a way to modify themes without the risk of losing customizations. This is done through the use of child themes. In this blog post, we’ll explore what child themes are, and how to create them in WordPress.
What is a Child Theme?
A child theme is a WordPress theme that inherits the functionality and styling of another theme, known as the parent theme. The child theme allows developers to make changes to the parent theme without affecting the original code. This means that customizations made to a child theme will remain intact even after updating the parent theme.
Child themes are useful for several reasons:
- Customization: With a child theme, developers can make customizations to the parent theme’s code without risking the loss of customizations during updates.
- Compatibility: Child themes are compatible with any WordPress theme, making them flexible and versatile.
- Security: Modifying a parent theme can be risky, as a single mistake can break the entire site. Child themes, on the other hand, provide a safe way to modify themes without any risk.
Creating a Child Theme
Creating a child theme in WordPress is a straightforward process that can be done in just a few steps. Here’s how to create a child theme in WordPress:
Step 1: Create a New Folder
The first step in creating a child theme is to create a new folder for the theme. The folder should be created in the wp-content/themes directory, which is located in the root folder of your WordPress installation.
In the themes folder, create a new folder and name it after your child theme. For example, if you want to create a child theme of the Twenty Twenty-One theme, you can name your folder twentytwentyone-child.
Step 2: Create a Stylesheet
Once you’ve created the child theme folder, you need to create a stylesheet for the theme. The stylesheet is used to define the theme’s properties, such as the theme name, author, and version.
To create a stylesheet, open a text editor and create a new file. Save the file as style.css and add the following code:
/*
Theme Name: Twenty Twenty-One Child
Theme URI: https://www.example.com/
Author: Your Name
Author URI: https://www.example.com/
Description: Child theme for Twenty Twenty-One
Version: 1.0
Template: twentytwentyone
*/
The code above defines the basic properties of the child theme. You should replace the values with your own information.
The most important line in the code is Template: twentytwentyone. This line tells WordPress that this is a child theme of the Twenty Twenty-One theme. Make sure that the Template line is set to the folder name of the parent theme.
Step 3: Enqueue Parent Theme Stylesheet
The next step is to enqueue the parent theme’s stylesheet in the child theme. This ensures that the child theme inherits the parent theme’s styles.
To enqueue the parent theme’s stylesheet, open the functions.php file in the child theme folder and add the following code:
<?php
function twentytwentyone_child_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'twentytwentyone_child_enqueue_styles' );
?>
This code registers and enqueues the parent theme’s stylesheet in the child theme.
Also, you can read to know Two Factor Authentication for Your WordPress Website.