2

So I am trying to make it so that you can load a class from a child theme, theme or plugin using the following:

class AisisCore_Loader_AutoLoader{

    protected static $_instance;

    protected static $_directories;

    public function get_instance(){
        if(null == self::$_instance){
            self::$_instance = new self();
        }

        self::$_directories = array(
            get_template_directory(),
            get_stylesheet_directory(),
            plugin_dir_path(__FILE__),
        );

        return self::$_instance;
    }

    public function reset_instance(){
        self::$_instance = null;
    }

    public function register_auto_loader(){
        spl_autoload_register(array($this, 'load_class'));
    }

    public function load_class($class){
        $path = str_replace('_', '/', $class);
        foreach(self::$_directories as $directories){
            if(file_exists($directories . '/' . $path . '.php')){
                require_once($directories . '/' . $path . '.php');
            }
        }
    }   
}

But I don't think I am understanding how the plugin_dir_path function is working. the problem is that, once this class is instantiated, no class can be loaded in the plugin's directory. that is /plugin/myplugin/ How ever if I have a theme using the framework I am developing, and the theme is activated, and I instantiate this class in the plugin's main php file via:

//require_once(plugin_dir_path(__FILE__) . 'AisisCore/Loader/AutoLoader.php');
require_once(get_template_directory() . '/AisisCore/Loader/AutoLoader.php');

// Setup the autoloader.
$auto_loader = AisisCore_Loader_AutoLoader::get_instance();
$auto_loader->register_auto_loader();

$obj = new AisisCore_Http_Http();

Then everything works, it sees the AisisCore_Http_http class and and so on, but if I do:

require_once(plugin_dir_path(__FILE__) . 'AisisCore/Loader/AutoLoader.php');
//require_once(get_template_directory() . '/AisisCore/Loader/AutoLoader.php');

// Setup the autoloader.
$auto_loader = AisisCore_Loader_AutoLoader::get_instance();
$auto_loader->register_auto_loader();

$obj = new AisisCore_Http_Http();

it freaks out and cannot find the class.

Ideas?

1
  • When you dump your require_once(plugin_dir_path(.. what does it look like?
    – Wyck
    Apr 13, 2013 at 21:46

1 Answer 1

1

when faced something similar I found a solution that works if your framework resides inside a plugin (so it seems).

If your main plugin file (one that is recognized by WordPress as the plugin file) is in, e.g. /plugin/Aisis/ you can put this path in a constant, eg.

define('AISISPATH', dirname(__FILE__));
require ( trailingslashit (AISISPATH) . 'AisisCore/Loader/AutoLoader.php' );
if ( ! class_exists('AisisCore_Loader_AutoLoader') ) die('Something strange happened.');
AisisCore_Loader_AutoLoader::get_instance();

In your get_instance() method of AisisCore_Loader_AutoLoader class put:

    self::$_directories = array(
        get_template_directory(),
        get_stylesheet_directory(),
        trailingslashit( AISISPATH )
    );

Doing like so, AisisCore_Loader_AutoLoader::$_directories[2] will be {wp_path_here}/plugin/Aisis so when you call $obj = new AisisCore_Http_Http(); your autoloader method will search in {wp_path_here}/plugin/Aisis/AisisCore/Http/Http.php.

Everything works, of course, if the plugin is activated from WP dashboard.

If you follow this process, is a good idea start the get_instance() method with: if ( ! defined('AISISPATH ') ) die('Framework not initilized.');

Your logic probably doesn't work because plugin_dir_path($file), is just a wrapper for trailingslashit( dirname( $file ) ) (see the Codex) so if you use this function passing __FILE__ as param it will return the folder in which the file that call the function is (in your case 'AisisCore/Loader/') and not your framework root.

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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