Overwrite WordPress PHP file in Child Theme SubFolder

Overwrite WordPress PHP file in Child Theme SubFolder

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.

  1. Login to the cPanel where you have hosted your WordPress blog. Open File Manager tool.
  2. Go to the child theme directory. You can find all the themes inside the WordPress directory public_html/wp-content/themes.
  3. Create a file with the name functions.php in the child theme folder if it is not there.
  4. Open functions.php to edit.
  5. Add the below line of code. (Suppose I want to override 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.

  1. Copy the 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.
  2. Modify the file /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.

4 Comments

  1. 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

Leave a Reply

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