WordPress / WordPress Themes
Creating and Using Child Themes
This tutorial covers the creation and usage of child themes in WordPress. You'll learn how to make modifications without risking changes to the original theme code.
Section overview
5 resourcesExplores themes, how to customize them, and create child themes.
Creating and Using Child Themes
1. Introduction
In this tutorial, we will learn how to create and use child themes in WordPress. Child themes allow us to make modifications to a parent theme without changing the original code. This way, we can customize our websites without the risk of our changes being overwritten by future updates to the parent theme.
By the end of this tutorial, you will be able to create a child theme, override parent theme files, and enqueue parent and child theme stylesheets.
Prerequisites
- Basic knowledge of WordPress
- Basic understanding of PHP, HTML, and CSS
- Access to a WordPress installation to practice and apply the concepts
2. Step-by-Step Guide
Child themes in WordPress are a safe way to modify a parent theme. Essentially, a child theme inherits all the features and appearance of its parent theme. You can make changes to the child theme without affecting the parent theme.
Creating a Child Theme
-
Create a new directory in your themes directory. The new folder will house your child theme files. It can be named anything but for best practice, it should be named similarly to the parent theme with
-childappended to the end. -
Create a
style.cssfile. This is the only required file in a child theme. It gives information about your child theme and inherits styles from the parent theme. -
Create a
functions.phpfile. This file adds features and functionality to the theme. In our case, it will be used to enqueue the parent and child theme stylesheets.
Using a Child Theme
To use a child theme, simply activate it in the WordPress admin dashboard just like any other theme. Any modifications or additions should be made in the child theme.
3. Code Examples
style.css
/*
Theme Name: Twenty Twenty-One Child
Theme URI: http://example.com/twenty-twenty-one-child/
Description: Twenty Twenty-One Child Theme
Author: John Doe
Author URI: http://example.com
Template: twentytwentyone
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: twenty-twenty-one-child
*/
This snippet is the header of the style.css file. The important part here is the Template: line which specifies the parent theme.
functions.php
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
?>
This PHP code is used to enqueue the parent theme's stylesheet. The child theme's style.css file is then enqueued with the parent theme as a dependency.
4. Summary
In this tutorial, you've learned how to create a child theme in WordPress and how to enqueue stylesheets for parent and child themes. You now have the ability to make modifications to your theme without risking changes to the original theme code.
Next Steps
To continue learning, you can try modifying other files in the parent theme such as header.php or footer.php. Just remember to copy them into your child theme before making changes.
Additional Resources
5. Practice Exercises
-
Create a child theme for the default WordPress theme. Enqueue the parent and child theme stylesheets.
-
Modify the site header in your child theme. Copy the
header.phpfile from the parent theme to your child theme and make a noticeable change (e.g., change the site title color). -
Add a new template file to your child theme. Create a
template-custom.phpfile and use it to display a custom page layout.
Solutions and Tips
-
Follow the steps in this tutorial to create a child theme and enqueue stylesheets.
-
To modify the site header, find the site title in the
header.phpfile and apply a different color using inline CSS or by adding a custom class and defining it in yourstyle.css. -
To create a custom template, write a PHP file that includes a custom loop or any other custom PHP code. To use the template, create a new page in WordPress and select your template under 'Page Attributes' on the right side of the editor.
Remember, the key to mastering WordPress child themes is practice. Keep experimenting with different modifications in your child theme.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article