0

With child themes we are supposed to leave parent functions.php in tact and extend it with the child functions.php. However parent functions.php typically define a whole load of include/requires which refer to files in the parent directory.

Do we need to remember to over-ride these in our child function.php each time we create a child.php that overrides the parent? Or does wordpress figure out the override at deeper level? The child functions.php runs before the parent functions.php so if require never link 2 files of the same name even if they are in separate directories then the child version will exclusively be selected. Otherwise we'd end up linking both child and parent php and get into a right bugger's muddle.

2
  • please edit your question. 1. reading rants is boring and they do not add anything to the question. 2. part of your question is documented in the codex and part has answers here. Please try to do the minimal research and then ask about whatever is left unclear. Nov 26, 2015 at 17:39
  • Its hardly 'a rant' and I think your being a bit sensitive if me calling best practice advice from wordpress HQ as sometime without explanation, but I take your point - its not really relevant to my core question and will edit the post accordingly.
    – PIp 1
    Nov 26, 2015 at 18:00

1 Answer 1

1

Ok, the first-thing I wanted to double-check is that rather than make any modifications to parent PHP, you simply create one with a duplicate name in the child directory which completely overrides the parent file?

No. You don't create a theme with the same name. You create a theme with whatever name you choose and provide the appropriate header in the style.css file to point to the parent.

/*
 Theme Name:   Twenty Fifteen Child
 Theme URI:    http://example.com/twenty-fifteen-child/
 Description:  Twenty Fifteen Child Theme
 Author:       John Doe
 Author URI:   http://example.com
 Template:     twentyfifteen
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Tags:         light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
 Text Domain:  twenty-fifteen-child
*/

The "Template" line is the one that points to the parent:

  • The Template line corresponds to the directory name of the parent theme. The parent theme in our example is the Twenty Fifteen theme, so the Template will be twentyfifteen. You may be working with a different theme, so adjust accordingly.

https://codex.wordpress.org/Child_Themes#How_to_Create_a_Child_Theme

Next...

Master stylesheet remains in parent but the live stylesheet will be from child which will typically import the parent stylesheet at the header of child CSS.

Yes. The child stylesheet will be read by WordPress Core for basic theme data, but still needs to be enqueued. The parent stylesheet will not be. It seemed to be pretty common to @import the parent and if I recall correctly the Codex used to suggest such a method. A CSS @import is not an efficient way to load a stylesheet though. It introduces lag in the page load as both stylesheets cannot load synchronously. A better way is that which now appears in the Codex using Core enqueue functionality:

function theme_enqueue_styles() {

    $parent_style = 'parent-style';

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style )
    );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

Anyway back to child themes... the main thing I am unclear about is that we're supposed to leave parent functions.php in tact. However parent functions.php typically define a whole load of include/requires which refer to files in parent directory.

That is correct, and that is why you leave that file alone (among other things).

Do you need to remember to over-ride these in your child function.php each time you create a child.php?

You only need to override the files and code that you need to override. The rest continues to function as normal based on the parent theme code. That is the point of the parent/child relationship and is why that structure is so powerful and flexible. You can leverage the code base of one theme to greatly reduce the effort needed to make another one.

The child functions.php run before the parent functions.php so the override sould work.

Here you have to be careful. You can only override code that is meant to be overridden. For the most part, that means files included via get_template_part() and related functions that are child-theme sensitive and functions wrapped in if(!function_exists)'...')) conditionals. Code include via PHP's require and include (or the *_once versions) cannot be overridden. Similarly with functions you can only override functions that are wrapped in if(!function_exists('...')) as you see used in the Core pluggable.php file. If you try to override any other function by providing one of your own of the same name you will get a fatal PHP error. PHP does not allow multiple functions of the same name.

You might be interested in this answer as well.

6
  • Thank you so much for your comprehensive and enlightening reply. I guess the n you've answered my question about how wordpress knows itsis using a child them and where the parent is: its read from the Template Line at th estop of the stylesheet. (and I always thought commented out material was never referenced by the PHP interpreter).
    – PIp 1
    Nov 26, 2015 at 19:08
  • The stylesheet isn't PHP. It wouldn't be read by the PHP interpreter at all under normal circumstance. WordPress has code to specifically open that file and parse the header, using it as a kind-of config file for the theme (which I consider a bit odd and in pretty bad form, honestly).
    – s_ha_dum
    Nov 26, 2015 at 19:11
  • Enqueueing the child stylesheet loads it twice. Check this Nov 26, 2015 at 19:14
  • I have never noticed that effect and can't duplicate it @PieterGoosen. I need to look into that. I know for certain that older version of WordPress did not load any stylesheets automatically, parent or child, and if you did not enqueue them the site would have little to no formatting.
    – s_ha_dum
    Nov 26, 2015 at 19:23
  • By main concern here is that we avoid including both a parent and a child version of the same file.php included conncurrently. In the instance where we override a parent file.php with a child file.php don't we need add a child include path for the new child.php into our child functions.php. By doing so, and because child functions.php runs before parent, the child.php is established and the parent function.php file.php 'require' statement is ignored. However this only works if 'require' bases its definition of duplicate attempts to include the same file on filename only and not path/filename.
    – PIp 1
    Nov 26, 2015 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.