When you create a child theme in WordPress it allows to customize the appearance and functionality of your website without modifying the parent theme directly. This ensures that your customizations are preserved even when the parent theme receives updates. Here’s a step-by-step guide on creating a child theme:
- Create a new folder: Create a new folder on your computer and give it a descriptive name for your child theme. For example, “mytheme-child”.
- Create a stylesheet file: Within the child theme folder, create a new file and name it “style.css”. This file will contain the styles specific to your child theme.
- Add required information to the stylesheet: Open the “style.css” file and add the following required information at the top:
/*
Theme Name: My Theme Child
Theme URI: [Your website URL]
Description: Child theme for My Theme
Author: [Your name]
Author URI: [Your website URL]
Template: mytheme [Replace "mytheme" with the folder name of the parent theme]
Version: 1.0
*/
/* Add your custom styles below this line */
Make sure to replace the relevant information with your own details.
Enqueue the parent theme’s styles: To inherit the styles from the parent theme, add the following code to the “functions.php” file of your child theme:
<?php
function enqueue_parent_theme_styles() {
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_parent_theme_styles');
?>
Customize the child theme: You can now add your custom CSS styles and any necessary template files within the child theme folder. Modify the styles in the “style.css” file to make your desired customizations.
Activate the child theme: Upload the child theme folder to your WordPress installation. Go to the WordPress admin dashboard, navigate to “Appearance” -> “Themes”, and you should see your child theme listed. Click on “Activate” to activate the child theme.
Your child theme is now active, and any changes you make to the styles or template files within the child theme folder will override the corresponding files in the parent theme. Remember to always make any customizations within the child theme to maintain their integrity during updates to the parent theme.
Note: If your parent theme includes custom templates or functions that you want to modify, you can create copies of those files in your child theme folder and make the necessary changes there. WordPress will use the child theme’s version of the file instead of the parent theme’s version.