A child theme is a WordPress theme that inherits the functionality and styling of another theme, called the parent theme. The child theme allows you to make modifications to the parent theme without directly modifying its files. This is particularly useful because it allows you to update the parent theme without losing your modifications.
Here’s how to create and install a child theme:
- Create a new folder in the “wp-content/themes/” directory of your WordPress installation. Name the folder something that describes your child theme (e.g. “mytheme-child”).
- Create a new file in your child theme folder called “style.css”. This file will contain the information about your child theme, including the name, description, and other details. Here’s an example:
/*
Theme Name: MyTheme Child
Theme URI: https://example.com/mytheme-child/
Description: MyTheme Child Theme
Author: Your Name
Author URI: https://example.com/
Template: mytheme
Version: 1.0.0
*/
/* Add your custom CSS below this line */
Note that the “Template” line should match the name of the parent theme directory.
- Create a new file in your child theme folder called “functions.php”. This file will contain any custom PHP code you want to add to your child theme. For example, if you want to enqueue a custom stylesheet or add a new widget area, you can do so in this file. Here’s an example:
<?php
// Enqueue a custom stylesheet
function mytheme_child_styles() {
wp_enqueue_style( 'mytheme-child-style', get_stylesheet_directory_uri() . '/style.css', array( 'mytheme-style' ), wp_get_theme()->get('Version') );
}
add_action( 'wp_enqueue_scripts', 'mytheme_child_styles' );
// Add a new widget area
function mytheme_child_widgets_init() {
register_sidebar( array(
'name' => __( 'MyWidget Area', 'mytheme-child' ),
'id' => 'mywidget-area',
'description' => __( 'Add widgets here to appear in the MyWidget Area.', 'mytheme-child' ),
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'mytheme_child_widgets_init' );
- Activate your child theme by going to Appearance > Themes in your WordPress dashboard. You should see your child theme listed there. Click the “Activate” button to activate your child theme.
That’s it! You can now make modifications to your child theme without affecting the parent theme.