Ability to override included files in child themes

  1. Ian Dunn

    One of the biggest limitations of child themes right now is that only the child theme's template files are automatically loaded. If you want to override a file that the parent theme includes (e.g., themedir/lib/foo.php) then you have to find the file that calls require_once(TEMPLATEPATH . '/lib/foo.php) and override it to call require_once(STYLESHEETPATH . '/lib/foo.php), and in some cases you have to do that 3 or 4 times until you get to the top of the hierarchy.

    It'd be better if there were some kind of wp_require_once() function that would take a path (relative to the theme dir), and then check if there was a child theme file to replace the parent theme one.

    function wp_require_once($path)
            require_once (STYLESHEETPATH . $path);
            require_once (TEMPLATEPATH . $path);

    Then theme developers could use wp_require_once('lib/foo.php') instead of require_once(TEMPLATEPATH . 'lib/foo.php'). If a child theme was being used and had a replacement file, WP would load it; otherwise it would load the parent file.

    Posted: 7 years ago #
  2. Robin174

    This is a great idea - are we any closer to it?
    Struggling to override 'require_once' and 'define' in parent theme to call a new file in a child theme...

    Posted: 5 years ago #
  3. WebmistressM

    Agreed. I too would like to see soem way to address parent themes with such a function.

    Posted: 4 years ago #
  4. Shonu


    Yes, just stumbled over this issue and now I need to change the original theme ;-(

    Posted: 4 years ago #
  5. acescence

    The ability to load any php file and override it via a child theme exists, via the locate_template function, but it's up to the parent theme developer to use this function to load files for you to be able to override those files in child themes.

    Posted: 4 years ago #
  6. fulippo


    One of the biggest limitations in the Child Theme approach is that you cannot use a OOP approach.

    Say that you have a class in the main template's functions.php and one in the child theme's functions.php. It would be really nice if you could just extend the main template's class and override only those methods you really need to change instead of adding some useless logic like:


    At this moment it is quite impossible because of the include order regarding the functions.php files.

    So +1 for this idea.

    Posted: 4 years ago #
  7. acescence

    @fulippo - this is not impossible and actually quite simple. just load your class file on the after_setup_theme action:

    function load_child_class(){
    require_once locate_template( 'child_class.php' );
    add_action( 'after_setup_theme', 'load_child_class' );

    Posted: 4 years ago #
  8. fulippo


    @acescence - thank you for your suggestion. This approach worked for me. There is only one problem when using this pattern: because child and parent themes are loaded sequentially you have to wrap instantiation logic to prevent script from loading two instances.

    This is what I did (I post it here for future readers who may adopt this approach):

    // Use this function to instantiate classes inside themes
    function my_theme_instance($class){
    // Check if we are inside child theme and action has not been called. This means that we are trying to instantiate parent theme
    if(is_child_theme() && did_action("after_setup_theme") === 0){
    return false;
    return new $class;

    Posted: 4 years ago #
  9. videogold

    Has this been solved? Child themes seem to lack the level of granularity needed of you can't override the styles and functions enqueued in the parent functions.php file.

    Posted: 3 years ago #
  10. Ipstenu (Mika Epstein)

    It's still complicated because not all themes account for it.


    Basically you have to remove the action/function and replace it in your own functions.php

    Posted: 3 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    18 Votes
  • Status

    This idea is under consideration