Overwrite WordPress PHP file in Child Theme SubFolder
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.
[Step-by-step] Overwrite WordPress PHP file in Child Theme subfolder
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.
- Login to the cPanel where you have hosted your WordPress blog. Open File Manager tool.
- Go to the child theme directory. You can find all the themes inside the WordPress directory
- Create a file with the name
functions.phpin the child theme folder if it is not there.
- Add the below line of code. (Suppose I want to override
my-file.phpfrom the parent theme which is located at
<?php require_once get_stylesheet_directory() . '/file/override/my-file.php'; ?>
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
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.
- Copy the
file/override/my-file.phpfrom parent theme to child theme. You must create the folder
override. The relative path of the file in parent and child theme should be the same.
- Modify the file
/file/override/my-file.phpas 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.