WordPress Child Theme Basics 3) Template overwriting


Example

The most common usage of a child theme is to override template parts. For example, a sidebar, if we have a theme with the following file at

/themes/template/sidebar.php

<?php
/**
 * The sidebar containing the main widget area.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 */ 

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}?>
<div id="sidebar" class="widget-area">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>

We can definitely add our own file in child theme (with the specifications from the first example) with the following file

/themes/child-theme/sidebar.php

<?php
/**
 * The sidebar containing the main widget area.
 */ 

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}?>
<div id="my-sidebar" class="my-own-awesome-class widget-area">
    <div class="my-wrapper">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </div>
</div>

Now my-own-awesome-class is safe in child theme and it won't be removed at any theme update and WordPress will always choose a template from child themes when it does find one on the same path.