WordPress.org

Ready to get started?Download WordPress

Forums

core function for child / parent theme file inclusion (5 posts)

  1. fubhy
    Member
    Posted 4 years ago #

    Hello everbody,

    while developing a parent / child theme setup for a multi-blog setup on one wp installation I had to work around some issues that came up while I was trying to include files from the child theme or -if not found there- from the parent theme.

    Lets say we have a plugin that checks the template folder for a file to include in order to make it theme specific:

    If u are not using a child / parent theme setup you would usually go and use the "get_template_directory()" function to get the path to the template in order to find and include the theme specific files. The Problem: If u have the child theme activated this function still points to the parent theme. So in order to include from your child theme you have to use a workaround like this: "dirname(get_bloginfo('stylesheet_url'))".

    In future releases I would like to see core functionality to ...

    • a) ... dinstinguish between child / parent theme (like "get_parent_template_directory()" or "get_child_template_directory()" or "has_parent_template()" or whatever...
    • b) ... include files from child / parent themes like include_from_template('path & filename'). This function then checks wether the file exists in the child theme, if not it includes the parent theme version of that file.
    • I hope that makes sense!

  2. fubhy
    Member
    Posted 4 years ago #

    this is an example of how I solved this with a theme function...

    function do_include($file)
    {
    	$parent = get_template_directory() . '/';
    	$child = $parent . '../' . get_child_folder() . '/' . $file;
    	$parent = $parent . $file;
    
    	if(file_exists($child))
    		include($child);
    
    	elseif(file_exists($parent))
    		include($parent);
    }
    
    function get_child_folder()
    {
    	return array_pop(explode('/', dirname(get_bloginfo('stylesheet_url'))));
    }
  3. C0BALT
    Member
    Posted 4 years ago #

    Have you submitted this to TRAC? I would really like this included in future versions of WordPress. If you haven't I will...

  4. dogbot
    Member
    Posted 3 years ago #

    This kida works for me:

    function child_theme_include($file){
        $parent = get_template_directory() . '/'.$file;
        $child = get_stylesheet_directory() . '/'.$file;
        if(file_exists($child))
            include($child);
        elseif(file_exists($parent))
            include($parent);
    }

    No extra functions

  5. tanver
    Member
    Posted 3 years ago #

    I found all this confusing unless a core function like get_child_dir() exists. I don't see any problem in using bloginfo('stylesheet_directory') to access the Child URL, and get_template_directory() OR TEMPLATEPATH - Assumption that templates still reside in the Parent Folder, which usually is the case.
    Is this not simple enough to use, at least it worked good for me and I am a beginner to the theme development and this was my first Child theme.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.