I hope you have already created the child theme in WordPress. Once you do that you can easily modify the files in the child theme parent directory. But you can not override/overwrite the files in subdirectories/sub-folders.
I faced the same problem while working on my blog. I search on Google and went through numerous articles. After learning, tweaking some lines of code, finally, I solved the problem.
Here is the step by step solution you can follow.
To make the changes or override the subdirectory files, you have to follow the steps given below.
You don’t need any programming experience. Even non-developer can follow these steps.
public_html/wp-content/themes
.functions.php
in the child theme folder if it is not there.functions.php
to edit.my-file.php
from the parent theme which is located at file/override/
directory.)<?php require_once get_stylesheet_directory() . '/file/override/my-file.php'; ?>
Here, get_stylesheet_directory()
returns the path of the activated theme directory. Currently, the activated theme is a child theme. So, it gives the path to the child theme directory
Note: get_template_directory()
gives the path to the parent theme directory.
You need to add this line code for all the files in sub-folders which you want to overwrite.
Here is the sample functions.php
file to overwrite three different files from three different directories.
file/override/my-file.php
from parent theme to child theme. You must create the folder file
and then override
. The relative path of the file in parent and child theme should be the same./file/override/my-file.php
as per your requirement.When you run the website, it will always refer my-file.php
file from the child theme instead of a file from the parent theme.
Wherever the changes you make in my-file.php
of the child theme, it will overwrite the changes in the parent theme.
You have to follow all these steps for overriding any file in parent theme.
Hope this helped you to overwrite WordPress PHP file in child theme subfolder. If it is not working for you or facing any challenges solving this issue, comment below.
Thank you very much! This helped me a lot.
You’re welcome!
Hi, unfortunately when you import a file, you also have to import all the references to which this file calls, it’s a real pain
Even I observed that. Sometimes the reference from the parent theme doesn’t work. This is really a pain.