WordPress.org

Ready to get started?Download WordPress

Ideas

Ability to override included files in child themes

  1. Ian Dunn
    Member

    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)
    {
        if( TEMPLATEPATH != STYLESHEETPATH && is_file(STYLESHEETPATH . $path) )
            require_once (STYLESHEETPATH . $path);
        else
            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: 3 years ago #
  2. Robin174
    Member

    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: 1 year ago #
  3. WebmistressM
    Member

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

    Posted: 10 months ago #
  4. Shonu
    Member

    +1

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

    Posted: 9 months ago #
  5. acescence
    Member

    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: 8 months ago #
  6. fulippo
    Member

    12345

    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:


    if(is_child_theme())
    do_mobile_stuff()
    else
    do_desktop_suff()

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

    So +1 for this idea.

    Posted: 8 months ago #
  7. acescence
    Member

    @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: 8 months ago #
  8. fulippo
    Member

    12345

    @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;
    }
    elseif(class_exists($class)){
    return new $class;
    }
    }

    Posted: 8 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    12 Votes
  • Status

    This idea is under consideration