WordPress: How to overwrite a sub-file in a Child Theme

Often, when working with Child Themes,  we find ourselves having to change some file in a sub-folder.

However, it is not enough to simply replicate the sub-folder and the sub-file, mirroring the Parent theme’s structure.

If the Parent’s functions.php is the file from which the include takes place, it will take priority over the Child’s folders and files structure.

For example, if we have a Parent Theme’s path like this: /parent-theme/subfolder/subfile.php we would think that simply replicating the same path in the Child Theme: /child-theme/subfolder/subfile.php would suffice, but it’s not the case.

We would then tend to go to our Child Theme’s functions.php file, and include there the new sub-file we just created/edited – correctlt placed in the Child Theme’s new sub-folder.

But this still won’t work, and if we place this in our Child Theme’s functions.php:

…or this:

…we’ll still see the Parent’s theme-subfile.php taking priority.

This happens because those functions get the Parent Theme’s path, and not the Child’s.

To adapt to this situation, we’ll need to include the sub-file this way in our Child Theme’s functions.php:

This other function, pasted in our Child’s functions.php, will be able to include the edited sub-file in the Child’s sub-folder.

5 people super-liked this.

Leave a Reply

Your email address will not be published. Required fields are marked *