1

I'm trying to include my parents themes functions.php include into my child theme functions.php.

I have a standard setup for a functions.php in my parent.

This is what i have in my child themes functions.php

<?php
/**
 * Base functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Base
 */

locate_template( array( '/vendor/autoload.php', '/functions.php' ), true, false );

the autoload.php file contains things like timber framework which works, it it doesn't seem to bring in the parent functions.php

Any help would be much appreciated.

Thanks Jake.

  • 1
    The parent theme’s functions.php is always loaded right after the child theme’s. Why do you want to load it twice? – fuxia Jan 12 '16 at 21:26
  • @toscho Thanks for your comment, is there anyway to load the parents functions.php in first then the childs functions.php. I'm hoping to use the parents functions file as a main file and use the childs functions file to add in additional settings etc... – Henshall Jan 12 '16 at 21:39
  • Just wait with your settings for after_setup_theme. The parent’s file will be loaded then. You shouldn't run code unconditionally anyway. – fuxia Jan 12 '16 at 23:03
  • @toscho How would i need to structure my child-theme functions.php to accommodate, any example would be much appreciated. Thanks Jake. – Henshall Jan 13 '16 at 0:11
  • What you would do depends on what the parent has already done. I'm not sure there is a general answer. – s_ha_dum Jan 13 '16 at 0:42
1

As per @toscho's comments, the parent theme is always loaded after the child theme's. In this way, if you want to override a parent theme's function you can rewrite it and the original will be ignored, whereas any functions you didn't rewrite are loaded and available.

There is no need to do it the other way around. If you are just loading additional files and defining actions and filters (as you should) there is no reason to worry about the order in which both function files are loaded, because at this point WP is just making an inventory of code to execute, not actually executing.

So, that gives you plenty of freedom to define additional settings in your child theme's functions.php, even if the parent's one has not been loaded yet.

  • Hey, thanks for the comment, so how would i go about allowing Kirki and Timber to be autoloaded for the child theme. Here's my file structure this is the line in my parents functions.php file to autoload the vendor autoload require __DIR__.'/../../../../vendor/autoload.php'; and this is whats in the childs functions.php function base_theme_setup() { locate_template( array( '../beast/functions.php' ), true, false ); } add_action( 'after_setup_theme', 'base_theme_setup', 11 ); – Henshall Sep 26 '16 at 11:23
0

I'm not entirely sure what you're trying to achieve, but it sounds like you're trying to overwrite functions from your parent theme in your child theme.

If this is the case you should make the parent function pluggable:

if ( ! function_exists( 'your_function' ) ) {

  function your_function() {
  }

}

Then you are able to define a function in your child theme with the same name. In this case, the child function will be used since it's loaded first, and the parent checks if the function exists.

function your_function() {
}

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.