Creating a child theme in WordPress is a good practice because it allows you to make customizations to your theme without losing those changes when the parent theme is updated. Here’s how you can create a child theme in WordPress:
Step 1: Create a New Theme Folder
Create a new folder: Navigate to the wp-content/themes directory in your WordPress installation using an FTP client or your hosting file manager. Inside the themes directory, create a new folder for your child theme. Give it a unique and descriptive name, usually, the name of the parent theme followed by “-child”. For example, if your parent theme is called “Twenty Twenty-One,” your child theme folder could be named “twentytwentyone-child”.
Step 2: Create the Stylesheet (style.css) File
Create a style.css file: Inside your child theme folder, create a file named style.css. This file is necessary for WordPress to recognize your theme.
Add the required header: Open style.css and add the following header information at the top of the file:
/*
Theme Name: Twenty Twenty-One Child
Theme URI: http://example.com/twentytwentyone-child/
Description: Twenty Twenty-One Child Theme
Author: Your Name
Author URI: http://example.com
Template: twentytwentyone
Version: 1.0.0
*/
- Theme Name: The name of your child theme.
- Theme URI: The URL of your child theme’s website.
- Description: A brief description of your child theme.
- Author: Your name or the name of your organization.
- Author URI: Your website or author’s website.
- Template: The directory name of the parent theme (case-sensitive).
- Version: The version number of your child theme.
Step 3: Create the Functions (functions.php) File
Create functions.php: Inside your child theme folder, create a file named functions.php.
Enqueue the parent theme stylesheet: Add the following code to your functions.php file to enqueue the parent theme’s stylesheet. This ensures that the styles from the parent theme are inherited by the child theme:
<?php
// Enqueue parent and child theme styles
function my_child_theme_enqueue_styles() {
// Load parent theme stylesheet first
wp_enqueue_style(‘parent-style’, get_template_directory_uri() . ‘/style.css’);
// Load child theme stylesheet
wp_enqueue_style(‘child-style’, get_stylesheet_uri(), array(‘parent-style’));
}
add_action(‘wp_enqueue_scripts’, ‘my_child_theme_enqueue_styles’);
?>
Step 4: Activate Your Child Theme
Activate your child theme: Go to your WordPress dashboard and navigate to Appearance » Themes. You should see your child theme listed there. Click on the “Activate” button to activate your child theme.
Now, you have successfully created and activated a child theme in WordPress. You can add custom styles, templates, and functionalities to your child theme without modifying the parent theme files directly. This approach ensures that your changes are preserved even when the parent theme is updated.
Happy Blogging!!