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.

Twenty Sixteen Theme

I am trying out the Twenty Sixteen Theme at my second WordPress practice site, wp.elizapro.com. I changed the background color and created a child theme.

Website home page with header imageMy child theme differs from the parent theme only in that there’s a custom CSS file.  More about Twenty Sixteen Theme:

  • Introduction
  • Download (this page includes a download button)
  • Features (it took me a while to find this page. Why?)
    • The “Features” are NOT described in the sidebar list entitled “Features.” Each of those links shows you a selection of WordPress themes that let you customize that feature … as far as I can tell
    • I can’t find further details on this theme’s features. I thought there was more than the custom colors and header described somewhere. In the meantime, as with any Theme, to see its theme-specific customizations go to your WP dashboard -> Appearance -> Themes, find your theme and hit Customize.