• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Overwrite WordPress PHP file in Child Theme SubFolder

Aniruddha Chaudhari/7044/4
WordPress

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.

phpWordPress
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    João
    January 3, 2023 at 11:51 pm

    Thank you very much! This helped me a lot.

    • Reply
      Aniruddha Chaudhari
      March 19, 2023 at 1:05 pm

      You’re welcome!

  • Reply
    John
    March 17, 2023 at 10:04 pm

    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

    • Reply
      Aniruddha Chaudhari
      March 19, 2023 at 11:48 am

      Even I observed that. Sometimes the reference from the parent theme doesn’t work. This is really a pain.

Leave a Reply Cancel reply

Interview Questions



You can share your interview experience.

Subscribe for FREE Newsletter

Do you want me to send you programing updates for FREE?

Subscribe below…

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard