Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wrote this method in a parent them that will auto load a class based on the class name, thus if the class name is: SomeFolder_Folder_Folder_Class.php then it will convert that to: SomeFolder/Folder/Folder/Class.php and load it if it exists:

public function load_class($class){
    $path = str_replace('_', '/', $class);
    if(file_exists(get_template_directory() . '/' . $path . '.php')){
        require_once(get_template_directory() . '/' . $path . '.php');
    }
}

This works in conjunction with spl_autoload_register() to achieve this 'auto load' functionality. So instead of having a bunch of require_once you just do: new SomeFolder_Folder_Folder_Class() and it auto loads it.

This works great in the parent them, and even in the child theme when loading classes from the parent theme. How ever, when trying to load classes from the child theme - it fails and states it can't find said class.

So I thought lets do:

public function load_class($class){
    $path = str_replace('_', '/', $class);
    if(is_child_theme()){
        if(file_exists(get_template_directory() . '/' . $path . '.php')){
            require_once(get_template_directory() . '/' . $path . '.php');
        }
    }else{
        if(file_exists(get_template_directory() . '/' . $path . '.php')){
            require_once(get_template_directory() . '/' . $path . '.php');
        }
    }
}

But then parent theme classes start freaking out moaning they can't be found, so I added a new level of complexity:

public function load_class($class){
    $path = str_replace('_', '/', $class);
    if(is_child_theme()){
        if(file_exists(get_stylesheet_directory() . '/' . $path . '.php')){
            require_once(get_stylesheet_directory() . '/' . $path . '.php');
        }else{
            if(file_exists(get_template_directory() . '/' . $path . '.php')){
                require_once(get_template_directory() . '/' . $path . '.php');
            }
        }
    }else{
        if(file_exists(get_template_directory() . '/' . $path . '.php')){
            require_once(get_template_directory() . '/' . $path . '.php');
        }
    }
}

But then were back to square one where you cant find the child theme class....

So - with that said, What should do? How do I get the auto loader to work in both the parent theme and the child theme?

Added level of complexity? child theme classes can extend and implement parent theme classes.

Thoughts?

share|improve this question
add comment

1 Answer 1

use locate_template() instead.

public function load_class( $class ) 
{
    $path = str_replace('_', '/', $class);
    // returns a path when something was found
    $located = locate_template( $path, TRUE );

    return $located ? TRUE : FALSE;
}

Always return FALSE in an autoloader if you haven’t found the class.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.