How to Override PHP Files in Your Child Theme

I figured how how to override an arbitrary file in a child theme. It is ALMOST as simple as placing the override file in the corresponding folder for the child theme. The step I didn’t know about, was to add a require_once() for the specific file in the child theme’s functions.php. Here is my example step by step:

My example is how to replace the font list in Customizr theme, with my own font list.

WordPress admin screen showing customizer for Customizr theme's Google Fonts setting

1) If I were to “hack” the parent theme to make this change, I’d change the file wp-content/themes/customizr/core/init-base.php

2) Make a copy of core/init-base.php, make your changes, and place it in the child theme’s folder: wp-content/themes/customizr-child/core/init-base.php.  Note you must create a “core” folder in which to place init-base.php — you must replicate the full path of the overridden file (or whatever file you refer to in step 3).

Screenshot of Windows Explorer
The overriding file is in the “core” folder

3) Modify your child theme’s functions.php to include the overriding file, by adding the following line:

require_once( get_stylesheet_directory() . 'core/init-base.php' );

Why? According to the WordPress codex: https://codex.wordpress.org/Child_Themes, about half way down, see …

Referencing / Including Files in Your Child Theme

When you need to include files that reside within your child theme’s directory structure, you will use get_stylesheet_directory(). Because the parent template’s style.css is replaced by your child theme’s style.css, and your style.css resides in the root of your child theme’s subdirectory, get_stylesheet_directory() points to your child theme’s directory (not the parent theme’s directory).

Here’s an example, using require_once, that shows how you can use get_stylesheet_directory when referencing a file stored within your child theme’s directory structure.

require_once( get_stylesheet_directory() . '/my_included_file.php' );

In my case, I’ll add this line to my child theme’s functions.php:

require_once( get_stylesheet_directory() . 'core/init-base.php' );

Screenshot of TextPad with Filezilla behind
Adding the “require_once” line to the child theme’s functions.php

4) Upload the new file and the modified functions.php to your web server, and the next time you access the font choices in the customizr, you’ll see your new font list.

4 thoughts on “How to Override PHP Files in Your Child Theme”

  1. Greetings: Is there a way to override wp-admin core files using the technique you described above as a way to “hack” the WP core files?

    For example, I would create a directory in my child theme:

    wp-content/themes/my-child-theme/wp-admin/user-edit.php

    And then add this to my child theme functions file:

    require_once( get_wp-admin_directory() . ‘/edit-user.php’ );

    This way, I can customize (hack) the edit-user file to my heart’s content … what you do you think?

  2. Hey there. Don’t know if you’ll reply.
    Basically, the method worked for me but it caused some unwanted changes for me (disrupted the layout of a page) although I only changed three lines of esc_html code. Any tips ?
    – I copied the whole php file and edited it from the child theme. or do I only copy the specific lines of code I want to edit?
    Note: I am not an expert in PHP

    1. Hi Abe,

      I’m still learning to moderate my blog comments, so I apologize for my late answer. Surely you’ve answered your question in another way now.

      I believe you want to copy the whole PHP file and edit it from the child theme. Since I wrote this blog post, I have one case where I didn’t need to add a require_once() for the specific file (header.php) in the child theme’s functions.php. So the method is still somewhat mysterious to me.

      I’ve also found that I can write functions in my child theme’s functions.php, and call them from a custom page template.

      Cheers,
      Carol

  3. Two other experienced WordPress developers at today’s Saratoga WordPress meetup said their experience was that their overrides worked without adding the require_once(), as in step 3.

    Maybe it depends on the theme. Customizr was the theme in my example. It already required non-standard code within the child theme’s functions.php, to get the parent theme’s style.css loaded BEFORE the child theme’s style.css.

    Maybe the require_once() as mentioned in the WordPress codex, was only an example? For sure, if I don’t place the require_once() statement in my functions.php, my child theme’s init_base.php file is NOT included.

Comments are closed.