__autoload in a plugin throws an error in WP – not able to locate translation_entry.class.php

Denis was pointing to the right direction, but just looking for if the file exists is not the whole job.

I do this by example, but you need to do this for every file that is unable to load:

  • The class that is unable to get auto-loaded, locate it. Class is Translation_Entry and it is located in /wp-includes/pomo/entry.php. That is some other library and not a core class.
  • You need to extend your autoloader to either deal with all cases -or- to ensure that wp-load.php got included before or while you register your autoloader.
  • Your autoloader is designed to load only classes bases on a specific scheme. And your autoloader is totally incompatible to any other autoloader. Instead, make use of spl_autoload_register() and register your base class autoloader. Then you can add another one upfront that is dealing with the other classes.

I’ve written some sample code that might not work, it’s untested. You might not even register multiple autoloaders in the end but just one (that calls the different subroutines instead), so this is just a quick written mock-up:

class MyAutoloader {
    public static function bootstrap() {
        spl_autoload_register(__CLASS__.'::loaderPomo');
        spl_autoload_register(__CLASS__.'::loaderWpClasses');
    }
    public static function getWpClassFile($name) {
        return sprintf('%s.class.php', strtolower($name));
    }
    public static function isWpClass($name) {
        $file = self::getWpClassFile($name);
        return (bool) file_exists($file);
    }
    public static function require($file) {
        require $file;
    }
    // standard wordpress .class.php file loader
    public static function loaderWpClasses($name) {
        if (self::isWpClass($name) {
            $file = self::getWpClassFile($name);
            self::require($file);
        }
    }
    // [...] write any code you need to handle other cases.
    // loader for pomo classes
    public static function loaderPomo($name) {
        if (self::isPomoClass($name)) {
            $file = self::getPomoFile($name);
            self::require($file);
        }
    }
}

Leave a Comment