Normally the functions.php in your CHILD theme is called before the PARENT functions.php. In some cases, you might like to alter functionality loaded through the PARENT theme in your CHILD theme, which is not possible without some changes.
From the WordPress Codex :”Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)”
For our new setup, I am using a base class in our PARENT theme to define all our defaults. Some of the CHILD themes need to overwrite or adjust functionality. This allows to have a pure foundation, than alter and extend as needed.
PARENT functions.php
is extending the TimberSite class in my case, as I am using Timber (TWIG for WordPress) for my themes
1 2 3 4 5 6 7 8 9 10 11 |
class cubicFusionBase extends TimberSite { function __construct(){ parent::__construct(); } } // If no CHILD theme is loading init the parent class if(CHILD_THEME_LOADED != 1){ new cubicFusionBase(); } |
CHILD functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Do not init the parent class define("CHILD_THEME_LOADED", 1); // Wait for theme setup to be finished add_action( 'after_setup_theme', function() { // Extend parent theme class class cubicFusionChild extends cubicFusionBase { function __construct() { // Call parent class constructor parent::__construct(); } /* Add or extend any other functionality here */ } // Init Child class new cubicFusionChild(); }, 42 ); |
Cheers
Alex